我正在使用bootstrap数据表进行分页.我还为每一行添加了click事件.但是click事件只在第一页中触发.排序或分页后它不起作用.这是我的 PHP代码来显示数据 table id='tblCustomers' class='table tabl
<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()); }); });将代码更改为以下.这是拨打 Event Delegation
$(function () { $("#tblCustomers").dataTable(); $(document).on('click',"#tblCustomers tr",function(){ alert($(this).find('td:first').text()); }); });