我使用引导数据表进行分页,我还向每个row.But添加了单击事件,单击事件仅在第一页中触发。它在排序后不工作,或者pagination.Here是我的php代码来显示数据。
<table id='tblCustomers' class='table table-bordered table-striped'>
<thead>
<tr>
<th>Customer id</th>
<th>Company</th>
<th>First name</th>
<th>Last name</th>
<th>Email</th>
<th>Last login</th>
<th>No Of logins </th>
</tr>
</thead>
<tbody>";
foreach ($this->result as $row) {
echo "<tr>
<td>{$row['customerid']} </td>
<td>{$row['company']} </td>
<td>{$row['firstname']} </td>
<td>{$row['lastname']} </td>
<td>{$row['email']} </td>
<td>{$row['lastlogin']} </td>
<td>{$row['count']}</td>
</tr>";
}
echo "</tbody></table>";
而jquery代码是
$(function () {
$("#tblCustomers").dataTable();
$("#tblCustomers tr").click(function(){
alert($(this).find('td:first').text());
});
});
发布于 2015-03-18 10:08:00
将代码更改为下面。这是呼叫Event Delegation
$(function () {
$("#tblCustomers").dataTable();
$(document).on('click',"#tblCustomers tr",function(){
alert($(this).find('td:first').text());
});
});
发布于 2015-03-18 10:07:48
尝试使用$("#tblCustomers tr").on('click', function(){...}
而不是常规的click
。
发布于 2015-03-18 11:07:26
请注意,即使是搜索和筛选也不会接受行单击事件。如果您在行上提供事件,如单击,双击。
在筛选器和搜索中,请确保调用包含所有自定义脚本的函数。
https://stackoverflow.com/questions/29119108
复制相似问题