喵喵時光機
2021-11-18 15:52:33
我有一個帶有 .on click 事件的代碼,效果很好。但是,如果我將其更改為窗口加載或文檔就緒,即使沒有數(shù)據(jù),它也會顯示“行中數(shù)據(jù)”。我很困惑為什么它適用于點擊事件而不是加載。評論將不勝感激,因為這讓我發(fā)瘋。非常感謝這有效$('#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 回答

繁星coding
TA貢獻1797條經(jīng)驗 獲得超4個贊
我認為問題是 $(this) 指的是第二種情況下的窗口。你應(yīng)該試試這個
$( 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();
}
});
});

繁星淼淼
TA貢獻1775條經(jīng)驗 獲得超11個贊
嘗試,應(yīng)該在加載 DOM 時觸發(fā)。
jQuery.ready(function(){
console.log("DOM loaded");
});
添加回答
舉報
0/150
提交
取消