dsx666666 2015-12-21 04:19
浏览 39
已采纳

onchange仅适用于表中的第一行

Am selecting an item from a select-option tag in a table to populate records from a database to fill another select-option tag, 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.

The HTML table

<table class="table table-bordered table-hover">
    <thead>
        <tr>
        <th width="2%"><input id="check_all" type="checkbox"/></th>
        <th width="10%">Credit Type</th>
        <th width="20%">Credit Account</th>
        <th width="15%">Fund</th>
        <th width="15%">Amount</th>
        </tr>
        </thead>
        <tbody>
         <tr>
            <td><input class="case" type="checkbox"/></td>
           <td>
             <select name="fund_id" class="form control" id="type_1">
             <option></option>
             <option value="23">Expense</option>
             <option value="3">Fixed Asset</option>
             <option value="8">Current Asset</option>
             <option value="5">Current Liability</option>
            <option value="4">Non-Current Liability</option>                                    
            </select>
          </td>
          <td>
            <select id="credit_1" name="cr_ac" class="form-control" >
             <option></option>
            </select>
         </td>
         <td> <input type="text" name="fund[]" id="fund_1" ></td>
         <td><input type="text"  name="amount[]" id="amount_1"></td>
      </tr>
     </tbody>
    </table>

The script that add new row to table

<script>
    var i=$('table tr').length;
    $(".addmore").on('click',function(){
        html = '<tr>';
        html += '<td><input class="case" type="checkbox"/></td>';
        html += '<td><select name="fund_id" class="form-control" id="type_'+i+'"><option></option><option value="23">Expense</option><option value="3">Fixed Asset</option><option value="8">Current Asset</option><option value="5">Current Liability</option><option value="4">Non-Current Liability</option></select></td>';
        html += '<td><select id="credit_'+i+'" name="cr_ac" class="form-control" ><option></option></select></td>';
        html += '<td><input type="text" name="fund[]" id="fund_'+i+'"></td>';
        html += '<td><input type="text" name="amount[]" id="amount_'+i+'"></td>';
       html += '</tr>';
        $('table').append(html);
        i++;
    });
</script>

The onchange function with ajax

<script>
for (var i = 1; i < 2; i++) {

    (function(i) {

       $(document).ready(function(){

          $("#type_"+i).change(function(){

            var id=$("#type_"+i).val();
            var dataString = 'id='+ id;

            $.ajax({
                   type: "POST",
                   url: "populate_debit_type.php",
                   data: dataString,
                   cache: false,
                   success: function(html){

                          $("#credit_"+i).html(html);
                   } 
            });

          });

       });

    })(i);

}
</script>

The PHP script that fetch the db records

<?php

include "includes/kon/db_connect.php";

    $id = $_POST['id'];
    // query for options based on value
    $sql = $link->query("SELECT * FROM accounts WHERE parent_id = '$id' OR ext_parent = '$id'");
    while($row = mysqli_fetch_array($sql)) {

       echo '<option value="'.$row['account_code'].'">'.$row['account_name'].'</option>';

   }
?>

Thanks

  • 写回答

2条回答 默认 最新

  • doqpm82240 2015-12-21 04:24
    关注

    Because you registered the change event before you dynamically create & inject the new rows. So the code you registered for the change event is only applicable to the items present in the page during your event registration. It won't work for future(dynamically injected to DOM) elements.

    For current and future elements to work, you should use jQuery on delegation.

    When you add a new row, give a css class name to your select element.

    $(".addmore").on('click',function(){
        //existing code
        html+= '<td><select class="form-control mySelect" id="type_'+i+'">'
    });
    

    and use this new css class as your jQuery selector to register the change event code using jQuery on method. Also in the success event on your ajax call to get the data for the second dropdown, you should use the closest() method to get a reference of the parent tr and use find() method to get a reference to the second dropdown.

    $(function(){
    
       $(document).on("change",".mySelect",function(e){
          e.preventDefault();
          var _this=$(this);
          // use _this, which is jQuery object of the dropdown user changed
          var id =_this.val();
    
         //Get the second dropdown
         var _crac=_this.closest("tr").find("select[name='cr_ac']");
    
         $.ajax({
                type: "POST",
                url: "populate_debit_type.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                   _crac.html(html);
               } 
          });
    
       });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题