gpt4 book ai didi

javascript - DataTables 更改所有页面上的复选框

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:00 26 4
gpt4 key购买 nike

我正在使用 DataTables 列出在我的 Web 应用程序的每个页面上显示的“事件”。

对于每一页,我都有一列,每个事件都是一行,每页都有复选框。 (让您了解它的外观:http://i.stack.imgur.com/6QhsJ.png)

当我点击一个页面时,它应该选中所有复选框,但是这些复选框仅在 DataTable 的当前页面上被选中。

感谢任何帮助!

编辑:这是我的问题的 JSFiddle (https://jsfiddle.net/2n3dyLhh/2/)

HTML

<table id="eventsTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Checkbox<input type="checkbox" id="checkall"/></th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td><input type="checkbox" id="checkbox1"/></td>
</tr>
<tr>
<td>Garrett Winters</td>
<td><input type="checkbox" id="checkbox2"/></td>
</tr>
<tr>
<td>Ashton Cox</td>
<td><input type="checkbox" id="checkbox3"/></td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td><input type="checkbox" id="checkbox4"/></td>
</tr>
<tr>
<td>Airi Satou</td>
<td><input type="checkbox" id="checkbox5"/></td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td><input type="checkbox" id="checkbox6"/></td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td><input type="checkbox" id="checkbox7"/></td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td><input type="checkbox" id="checkbox8"/></td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td><input type="checkbox" id="checkbox9"/></td>
</tr>
<tr>
<td>Sonya Frost</td>
<td><input type="checkbox" id="checkbox10"/></td>
</tr>
<tr>
<td>Jena Gaines</td>
<td><input type="checkbox" id="checkbox11"/></td>
</tr>
<tr>
<td>Quinn Flynn</td>
<td><input type="checkbox" id="checkbox12"/></td>
</tr>
</tbody>
</table>

JavaScript

$(document).ready(function() {
$.extend($.fn.dataTable.defaults, {
"columns": [null, { "orderable": false }]
});
$('#eventsTable').DataTable();
});

$("#checkall").on('click', function() {
if (this.checked) {
for(var i = 1; i <= 12; i++) {
var id = "#checkbox" + i;
$(id).prop('checked', true);
}
} else {
for(var i = 1; i <= 12; i++) {
var id = "#checkbox" + i;
$(id).prop('checked', false);
}
}
});

最佳答案

您的点击处理程序应更改为:

$("#checkall").on('click', function () {
$('#eventsTable').DataTable()
.column(1)
.nodes()
.to$()
.find('input[type=checkbox]')
.prop('checked', this.checked);
});

参见 this example用于代码和演示。

考虑使用 jQuery DataTables Checkboxes以便更轻松地处理由 jQuery DataTables 提供支持的表格中的复选框。

关于javascript - DataTables 更改所有页面上的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30328556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com