我有一个带有 .on click 事件的代码,效果很好。但是,如果我将其更改为窗口加载或文档就绪,即使没有数据,它也会显示“行中数据”。我很困惑为什么它适用于点击事件而不是加载。评论将不胜感激,因为这让我发疯。非常感谢这有效$('#nirqst').on('click', 'tr', function () {var table = $('#nirqst').DataTable(); //get the current row var currentRow = $(this).closest("tr"); var col1 = currentRow.find(".dataTables_empty").html(); if((col1)=='No data available in table') { console.log(col1); table.buttons().disable(); } else { console.log('data in row'); table.buttons().enable(); }});这不$( window ).on( "load", function() {var table = $('#nirqst').DataTable(); //get the current row var currentRow = $(this).closest("tr"); var col1 = currentRow.find(".dataTables_empty").html(); if((col1)=='No data available in table') { console.log(col1); table.buttons().disable(); } else { console.log('data in row'); table.buttons().enable(); }});
2 回答
![?](http://img1.sycdn.imooc.com/5c4aa098000126bb09600960-100-100.jpg)
繁星coding
TA贡献1797条经验 获得超4个赞
我认为问题是 $(this) 指的是第二种情况下的窗口。你应该试试这个
$( window ).on( "load", function() {
$('#nirqst tr').each(function () {
var table = $('#nirqst').DataTable();
var col1 = $(this).find(".dataTables_empty").html();
if((col1)=='No data available in table') {
console.log(col1);
table.buttons().disable();
} else {
console.log('data in row');
table.buttons().enable();
}
});
});
添加回答
举报
0/150
提交
取消