我正在编写一些jquery来转换数据表http://www.datatables.net/的行 进入可点击的链接。单击时,jquery从隐藏的第一列中获取值,并将其附加到链接。
我有这个工作,但是,当没有返回记录时。它仍然会生成一个可点击的行,但它没有值,会创建一个死链接。
所以我想让所有行都可以点击,除了没有返回记录时创建的行。
如果没有返回记录,则该行包含一个class ='dataTables_empty'的单元格。所以我想排除这一行。
所以我需要指定一个选择器,它允许我选择表中的所有行,除了带有一个dataTables_empty类的td的行。 这是我的尝试,如果有人可以帮助我,将不胜感激。
$("#table_list").on("click", "tbody tr:not('td .dataTables_empty')", function (e) {
var id = table.fnGetData(this, 0);
document.location.href = "test.cshtml?ID=" + id;
});
答案 0 :(得分:1)
您可以将:has
与:not
:
$("#table_list").on("click", "tbody tr:not(:has(.dataTables_empty))", function (e) {
var id = table.fnGetData(this, 0);
document.location.href = "test.cshtml?ID=" + id;
});
答案 1 :(得分:0)
这似乎有效:
$("#table_list").on("click", "tbody tr:not('> td.dataTables_empty')", function (e) {
var id = table.fnGetData(this, 0);
document.location.href = "test.cshtml?ID=" + id;
});
您还可以使用datatables提供的render方法将每个列呈现为实际链接,并且您无需将jquery绑定到由datatables生成的任何单元格。 http://datatables.net/reference/option/columns.render
$('#example').dataTable( {
"columnDefs": [ {
"targets": 0,
"data": "download_link",
"render": function ( data, type, full, meta ) {
var id =full[0];
return "<a href='test.cshtml?ID="+ id +"'>"+data+"</a>";
}
} ]
} );