doujiu3882 2016-11-17 04:44
浏览 30
已采纳

更改仅适用于表中的第一行

I am selecting an item from a select-option tag in a table to populate records from a database to fill another input field, but unfortunately, it only works for the first table row. When I add another table row with jquery, it does not work in the new added table row.

        <table class="table table-borderd table-hover">
            <thead>
                <tr>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Quantity</th>

                    <th>Discount %</th>
                    <th>Total</th>
                    <th><input type="button" class="btn btn-primary addmore" value="+"></th>
                </tr>
            </thead>
            <tbody id="itemlist2" class="detailss">
                <tr>
                    <td>
                        <select class="select-product form-control"  name="product_name[]">
                          <option value="">
                  </option>
                     <?php
                    foreach ($product as $key ):
                      ?>
                     <option value="<?php echo $key['id']  ?>">
                       <?php echo $key['product_name'] ?>
                     </option>
                    <?php  endforeach; ?>
                        </select>
                    </td>

                    <td>   
                        <input type="text" name="price[]"  class="price form-control" value="">
                   </td>

                    <td><input type="text"  name="quantity[]" class="form-control"></td>
                    <td><input type="text"  name="discount[]" class="form-control"></td>
                    <td><input type="text"  name="total[]" class="form-control"></td> 
              </tr>
          </tbody>
        </table>

The script that add new row to table

<script>
    var i=$('#itemlist2 tr').length;
    $(".addmore").on('click',function(){
        html = '<tr>';
        html += '<td><select class="select-product form-control" name="product_name[]">  <option value=""> </option><?php foreach ($product as $key ): ?><option value="<?php echo $key['id']  ?>"><?php echo $key['product_name'] ?></option><?php  endforeach; ?></select></td>';
        html += '<td><input type="text" name="price[]"  class="price form-control" value=""></td>';
        html += ' <td><input type="text" name="quantity[]" class="form-control"></td>';
        html += '<td><input type="text"  name="discount[]" class="form-control"></td>';
        html += '<td><input type="text"  name="total[]" class="form-control"></td>';
       html += '</tr>';
        $('#itemlist2').append(html);
        i++;
    });
</script>

The onchange function with ajax

        <script>
$(function() {

$(document).on('change', '.select-product',  function(){
          $(this).change(function(){
            var id=$(this).val();
            var dataString = 'id='+ id;
            $.ajax({
                   type: "GET",
                   url: "<?php echo site_url('/product/view_data'); ?>",
                   data: dataString,
                   cache: false,
                   success: function(html){
  $('.price').parent().parent().find('.price').val(html); 
                   } 
            });

          });

       });

    });

</script>

Thanks for your time!

  • 写回答

3条回答 默认 最新

  • douzhicai7148 2016-11-17 06:20
    关注

    Use delegate for dynamically added element. For example

    $(document).delegate( ".select-product", "change", function() {
          $(this).change(function(){
            var id=$(this).val();
            var dataString = 'id='+ id;
            $.ajax({
                   type: "GET",
                   url: "<?php echo site_url('/product/view_data'); ?>",
                   data: dataString,
                   cache: false,
                   success: function(html){
    
                    alert(html);
                        //  $("#price").val(html);
                   } 
            });
    
          });
    
       });
    
    });
    

    I hope it will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上