【问题标题】:change only works for the first row in a table更改仅适用于表中的第一行
【发布时间】:2016-11-17 04:44:40
【问题描述】:

我从表中的选择选项标签中选择一个项目来填充数据库中的记录以填充另一个输入字段,但不幸的是,它仅适用于第一个表行。当我用 jquery 添加另一个表行时,它在新添加的表行中不起作用。

        <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>

向表格添加新行的脚本

<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>

ajax的onchange函数

        <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>

感谢您的宝贵时间!

【问题讨论】:

    标签: javascript php jquery mysql ajax


    【解决方案1】:

    对动态添加的元素使用委托。例如

    $(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);
                   } 
            });
    
          });
    
       });
    
    });
    

    希望对你有帮助。

    【讨论】:

    • 先生,我的代码几乎可以正常工作,除了每个字段中的重复值。例如,当我选择产品时,每次我声明 ID 价格 $("#price").val(html) 的每一行都会设置 ajax 值。我想当我选择第一行产品时,价格将设置在价格框的第一行,当我从第二行选择产品时,价格将显示在价格框的第二行。希望你能理解我的问题。
    • 先生,您能帮帮我吗?从过去的 8 天开始,我一直在受苦。
    • 你应该使用 "price" 作为类而不是 id 然后使用这个 $(this).parent().parent().find('.price').val(html);为相应的行设置价格。抱歉耽搁了
    • 先生,我已更改,但它会同时更新多个价格框。
    • 先生,我已经更新了 html 和 ajax 代码。请看一看。
    【解决方案2】:

    新添加的元素没有事件绑定。

    最快的解决方法是更改​​您的事件绑定代码

    $('input').change(function () {
      // Do some stuff.
    });
    

    $(document).on('change', 'input', function() {
      // Do some stuff.
    });
    

    【讨论】:

      【解决方案3】:

      检查以下几行:

      html += '<td><select class="select-product form-control" id="product" ....
      

      var id=$("#product").val();
      

      您正在为多行分配一个 ID,并尝试使用唯一的 id 获取值。这就是为什么您只获取第一行的数据。

      要解决这个问题,请使用 class$(this) 引用它以获取当前实例的值。

      【讨论】:

      • 我已经改变了,但是如果我点击多个选择元素,则值设置在第一行。
      猜你喜欢
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2015-12-09
      • 1970-01-01
      • 2019-01-12
      相关资源
      最近更新 更多