我知道这可能看起来很原始,但我一整天都在尝试实现它,也许是因为我无法完全理解如何使用 API,我正在使用 DataTables 1.10.0,我有一个具有分页功能的表格,每一行都有一个复选框,我需要一个“检查所有按钮”来检查所有页面中的所有复选框,问题是它只检查当前页面中的复选框,并保留其他页面未选中,这应该很容易,但我无法弄清楚!我发现的答案使用“fnGetNodes”,它似乎已被弃用,并且版本 1.10 未使用
编辑:这是我的标记
<table class="table table-striped table-bordered table-hover" id="numbers_table">
<thead>
<tr>
<th><input type="checkbox" id="checkall" title="Select all" onClick="toggle(this)"/></th>
<th>Number</th>
<th>Company</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
<% _.each(array, function (value) { %>
<tr>
<td><input type='checkbox' name='numbers[]' value='<%=value.id%>'/></td>
<td><%= value.number %></td>
<td><%= value.company %></td>
<td><%= value.tags %></td>
</tr>
<% }) %>
</tbody>
</table>
<button type="button" class="btn btn-primary" id="checkall2">SELECT ALL</button>
<script>
$(document).ready(function() {
$('#numbers_table').dataTable({
//"bPaginate": false,
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0 ] }
]
});
$("#checkall2").click(function() { // a button with checkall2 as its id
$(':checkbox').prop('checked', true); // all checkboxes, you can narrow with a better selector
});
});
</script>
最佳答案
这应该适合你
var table = $('#numbers_table').DataTable();
$('#checkall').click(function () {
$(':checkbox', table.rows().nodes()).prop('checked', this.checked);
});
关于jquery - 数据表 1.10 "Check all"通过 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23637586/