【问题标题】:Checkboxes are not working after adding rows dynamically to the datatable将行动态添加到数据表后,复选框不起作用
【发布时间】:2018-03-25 15:41:48
【问题描述】:

我使用 AJAX 将多行添加到包含复选框的数据表中 作为一个元素。我为之后的复选框编写了代码 选择/取消选择“全选”,tbody 中的所有子复选框都应该得到 相应地选中或取消选中。在手动选择/取消选择任何子复选框后,“全选”复选框应更改。但这部分不起作用。

HTML 代码:

<table id="user" class="w3-table-all w3-hoverable w3-card-4 display">
                <thead>
                    <tr class="w3-light-grey">
                        <th><input type="checkbox" class="w3-check" id="select-all"></th>
                        <th>User Id</th>
                        <th>Username</th>
                        <th>Full Name</th>
                        <th>Activated Date</th>
                        <th>Last Login Date</th>
                    </tr>
                </thead>
                <tbody id="body">

                </tbody>
</table> 

用于复选框的 JQuery:

$("#select-all").change(function(){ 
        $(".checkbox").prop('checked', $(this).prop("checked"));
    });


    $('.checkbox').change(function(){ 
        if(false == $(this).prop("checked")){ 
            $("#select-all").prop('checked', false); 
        }

        if ($('.checkbox:checked').length == $('.checkbox').length ){
            $("#select-all").prop('checked', true);
        }
    });

数据表和 AJAX 函数代码:

var table =  $('#user').DataTable({
        "columns": [
            { "orderable": false },
            null,
            null,
             null,
             null,
            null,
          ],
        "order": [[ 1, "asc" ]]
     });

 $.ajax({
url: 'data.json',
dataType: 'json',
success: function(data) {
    for (var i=0; i<data.length; i++) {
        if(data[i].enabled==1)
        var row1 = $('<tr><td><input type="checkbox" class="w3-check checkbox" value=""></td><td>' + data[i].id+ '</td><td>' + data[i].username + '</td><td>' + data[i].first_name + ' ' + data[i].middle_name + ' ' + data[i].last_name + '</td><td>' + data[i].activated_date + '</td><td>' + data[i].last_login + '</td></tr>');
      table.rows.add(row1);
        table.draw();
        $("#body").append(row1);  
    }
},
error: function(jqXHR, textStatus, errorThrown){
    alert('Error: ' + textStatus + ' - ' + errorThrown);
}
});

【问题讨论】:

    标签: jquery ajax datatables


    【解决方案1】:

    试试这个代码

    $("#user").on("click", "#select-all").change(function(){ 
            $(".checkbox").prop('checked', $(this).prop("checked"));
        });
    
    $("#user").on("click", ".checkbox", function() {
        $("#select-all").prop('checked', true)
        $("#user").find(".checkbox").each(function() {
          if (!$(this).prop('checked')){
            $("#select-all").prop('checked', false);
          return;
         }
        })
      })
    

    这里是示例代码

    $(function() {
    
      //handler for selectall change
      $('#selectAll').change(function() {
        $("input[name='msisdn[]']").prop('checked', $(this).prop('checked'))
      })
    
      //handler for all checkboxes to refect selectAll status
      $("input[name='msisdn[]']").change(function() {
        $("#selectAll").prop('checked', true)
        $("input[name='msisdn[]']").each(function() {
          if (!$(this).prop('checked')){
            $("#selectAll").prop('checked', $(this).prop('checked'));
            return;
          }
        })
      })
    
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="multiple_msisdn">
      <label class="exp">
    <input type="checkbox" class="exp" id="selectAll"> -- Select All -- </label>
      <label class="exp">
    <input type="checkbox" class="exp" name="msisdn[]" value="1">&nbsp;ABC [xxxxxxxx]</label>
      <label class="exp">
     <input type="checkbox" class="exp" name="msisdn[]" value="18">&nbsp;ABC [xxxxxx]</label>
    </div>

    【讨论】:

    • 尝试了您的代码..它仅适用于已编写的 tbody 行,就像您的示例代码一样。但是通过ajax添加行后它失败了。在取消选择/选择“全选”时,tbody 复选框正在完美改变,但在取消选择任何 tbody 复选框后,“全选”并没有改变
    • 尝试创建一个 jsFiddle
    • 它只是您给定的代码..您已经编写为示例..这是另一个 jsfiddle 链接..请帮助我jsfiddle.net/0j7e8wpx/15
    • 我在上面.. 但示例代码供您参考我还在回答开始时提供了与您的模板相关的实际代码
    • 你能告诉我如何为这个复选框分配不同的值,将其存储在数组中并将其发送到服务器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2020-12-20
    • 2013-09-09
    相关资源
    最近更新 更多