我的数据表将数据加载近5页。我可以访问第一页中的隐藏列。但是当我点击下一页中的行时,点击事件不会被捕获。这是我的代码:
$(document).ready(function () {
$("#dataobjects").dataTable({
"sPaginationType": "full_numbers",
"bJQueryUI": true,
"aoColumns": [
/* Client */ null,
/* Check */ null,
/* Desc */ null,
/* System Use(in %) */ null,
/* Valid */ null,
/* Result */ null,
/* TimeStamp */ null,
/* Facts */ { "bSearchable": false,
"bVisible": false },
]
});
oTable = $('#dataobjects').dataTable( );
$("#dataobjects tbody tr").click( function( e ) {
if ( $(this).hasClass('row_selected') ) {
$(this).removeClass('row_selected');
} else {
oTable.$('tr.row_selected').removeClass('row_selected');
$(this).addClass('row_selected');
// Get the data from the selected row
var fid= oTable.fnGetData( this, 7 );
alert("Output: " + fid);
}
});
答案 0 :(得分:2)
在您到达下一页之前,tr元素不存在;尝试在table元素上使用事件委托:
$("#dataobjects").on('click', 'tbody tr', function( e ) {
if ( $(this).hasClass('row_selected') ) {
$(this).removeClass('row_selected');
} else {
oTable.$('tr.row_selected').removeClass('row_selected');
$(this).addClass('row_selected');
// Get the data from the selected row
var fid= oTable.fnGetData( this, 7 );
alert("Output: " + fid);
}
});