duanjucong3124 2016-02-07 02:03 采纳率: 100%
浏览 38
已采纳

对多个选择使用ajax选择更改

I have a table row where I added the ability to add/remove using .clone and .remove

I also have an ajax call to my controller in code igniter to grab the data and populate the other inputs based on data.

It currently works one the first one, I am able to select an option from the dropdown and it fills the values of the neighboring inputs. My problem is when I click add and it successfully clones the row, when I change that dropdown its like the .change and .ajax events no longer work.

Here is my jQuery which is here I think I am having the problem:

$('.invoice_line_item').each(function(){
    $(this).change(function(){

        var table = $(this).closest('.tr_clone');

        var select_value = $(this).val();
        //alert(item_id + ' ' + select_value);
        console.log(item_id + select_value);

         $.ajax({
            url: '<?php echo site_url("Invoice_item/get_item"); ?>',
            type: 'POST',
            dataType: "JSON",
            data: ({'id' : select_value}),
            success: function (data, status)
            {
                //alert('Success ' + data);
                console.log(data);
                $(table).find('.description').val(data['work_description']);
                $(table).find('.amount').val(data['amount']);
                $(table).find('.quantity').val(data['quantity']);
                $(table).find('.price').val(data['amount']);
            },
            error: function (data, xhr, desc, err)
            {
                alert('An Error Occurred: ' + xhr + ' ' + desc + ' ' + err);
                console.log(data);
            }
        }); 
    });
});

Here is the HTML:

<tbody>
  <tr class="tr_clone" id="inv_line_1">
    <td>
        <select id="line_item_1" name="invoice_line_item" class="invoice_line_item">
            <option></option>
            <?php
    foreach($prefix_line_items as $line_item)
    {
        ?>
        <option value="<?php echo $line_item['id']; ?>"><?php echo $line_item['item']; ?></option>
        <?php
    }
    ?>
        </select>
    </td>
    <td><input type="text" id="description_1" name="description" class="description" value="" /></td>
    <td><input type="currency" id="amount_1" name="amount" class="amount" value="" /></td>
    <td><input type="number" id="quantity_1" name="quantity" class="quantity" value="" /></td>
    <td><input type="currency" id="price_1" name="price" class="price" value=""  readonly/></td>
    <td><a href="#" onclick="return false;" class="add-line-item"><i class="fa fa-plus"></i></a>   <a href="#" onclick="return false;" class="remove-line-item"><i class="fa fa-minus"></i></a></td>
  </tr>
</tbody>

No errors in console, works the first time for the first row but when I click the Add button and it duplicates the row, I change the (cloned) select element and its like the jQuery no longer fires so it isnt grabbing the data via ajax or printing to the console.

  • 写回答

1条回答 默认 最新

  • dongqiao1888 2016-02-07 02:11
    关注

    While cloning the element, by default, it does not copy the event handlers to new elements. For that you have to pass true to tell clone method to do so. Good practice is to clone the element and then copy only required events manually to the new element.

    Check below example given on jquery's website,

    / Original element with attached data
    var $elem = $( "#elem" ).data( "arr": [ 1 ] ),
        $clone = $elem.clone( true )
          

    Hope it helps..

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)