当我点击表格标题中的"全部"复选框时,如何检查数据表中的所有复选框?

Mad*_*vda 1 jquery jquery-datatables

我已经实现了一个数据表的例子.当我点击数据表标题中的"全部"复选框时,它只检查并取消选中一次.第二次它无法检查表体中的所有复选框.我的代码如下:

JSFIDDLE链接点击这里

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.0-beta.1/js/jquery.dataTables.js"></script>
    <link href="http://cdn.datatables.net/1.10.0-beta.1/css/jquery.dataTables.css" rel="stylesheet">
</head>

<body>
    <h1>DataTable Checkbox</h1>
    <table class="display dataTable" id="flow-table">
        <thead>
            <th class="check">
                <input type="checkbox" id="flowcheckall" value="">&nbsp;All</input>
            </th>
            <th>Name</th>
            <!-- <th>Cookie</th> -->
            <th>Priority</th>
        </thead>
        <tbody>
        </tbody>
    </table>
</body>
<script>
    $(document).ready(function () {
        var str = "";
        for (i = 0; i < 25; i++) {
            a = i + 1;
            str += "<tr><td><input type='checkbox' id='checkall' name='mydata' value=" + a + "></td><td>a" + a + "</td><td>name" + a + "</td></tr>";
        }
        $("#flow-table > tbody").append(str);
        oTableStaticFlow = $('#flow-table').dataTable({
           "aoColumnDefs": [{
                         'bSortable': false,
                         'aTargets': [0]
                      }],
        });

        $("#flowcheckall").click(function () {
            if (this.checked) {
                //alert("checked");
                //$('input', oTable.fnGetNodes()).attr('checked',this.checked);
                $('#flow-table tbody input[type="checkbox"]').attr('checked', true);
            } else {
                //alert("unchecked");
                $('#flow-table tbody input[type="checkbox"]').attr('checked', false);
            }
        });
    });
</script>

</html>
Run Code Online (Sandbox Code Playgroud)

有人帮帮我..!

小智 6

您只需使用此功能选中/取消选中所有复选框即可:

  $("#flowcheckall").click(function () {
        $('#flow-table tbody input[type="checkbox"]').prop('checked', this.checked);
    });
Run Code Online (Sandbox Code Playgroud)

小提琴