Datatables实例与DOM中复选框状态不同步问题求助
Datatables实例与DOM中复选框状态不同步问题求助
大家好,我碰到了一个Datatables和DOM复选框状态不同步的问题,想请教一下各位:
我有一个名为my_tbl的表格,是用Datatables 2.1.8这个jQuery库初始化的,因为表格模板不在当前HTML文件里,所以用了Python Django的模板语法引入:
/* using python django template because table template is not in the same html file */ {% include 'my_tbl_template.html' %} let my_tbl=$('#my_tbl').DataTable({...}),
表格的每个单元格里都包含这样的复选框元素:<input type="checkbox" value="some_value">,这些复选框有初始的选中或未选中状态。
现在的问题是:当我手动修改复选框的选中状态(勾选或取消勾选)后,my_tbl这个Datatables实例并没有和HTML DOM同步!
举个例子:初始状态下有两个复选框都是选中的,当用户手动取消其中一个的选中状态后:
printCheckboxStatusDom()会输出 1(这是预期的当前选中复选框数量)- 但
printCheckboxStatusTbl()却输出 2(还是初始的选中数量)
下面是我的相关代码:
$(document).ready(function(){ $('input[type=checkbox]').change(function() { printCheckboxStatusDom(); // prints correctly how many checkbox is checked at this time printCheckboxStatusTbl(); //always prints initialized values (no change) }); function printCheckboxStatusDom(){ let checkeds = $(document).find('input[type=checkbox]:checked'); console.log('DOM: checked boxes: ' + checkeds.length); } /* Function to print checkboxes status in the my_tbl instance */ function printCheckboxStatusTbl() { my_tbl.rows().every(function (rowIdx, tableLoop, rowLoop) { let rowNode = this.node(); let cellCheckboxes = $(rowNode).find('input[type=checkbox]:checked'); console.log('Tbl: checked boxes: ' + cellCheckboxes.length); }); } });
备注:内容来源于stack exchange,提问作者Bill Choi




