窗口加载未显示正确的 else 消息

我有一个带有 .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();

    }

});

喵喵时光机
浏览 140回答 2
2回答

繁星coding

我认为问题是 $(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();    }  });});

繁星淼淼

尝试,应该在加载 DOM 时触发。jQuery.ready(function(){  console.log("DOM loaded");});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript