数据表链接在第一页后停止工作


Datatable link stops working after page one

我有一个数据表,里面有一个data-href来创建一个链接到论坛页面,我的问题在于我的数据表链接在第一页上工作得很好,但是当我转到第二页时,我不能再跟随链接,但它仍然有一个链接的所有数据,当我检查元素。我的JS和样式的链接在数据表

<style>
        tr.clickable-row { cursor: pointer; }
        tr.clickable-row:hover { background-color:#F0F8FF;}
        </style>
        <script>
        jQuery(document).ready(function($) {
    $(".clickable-row").on('click',$('.clickable-row'),function() {
        window.document.location = $(this).data("href");
    });
});
</script>

所有的行都被MySQL表调用来填充数据,并被正确地拉出。

echo "<tr class='clickable-row' data-href='viewPage.php?id=".inv_forum_page()."&forum=".$forum['id']."'>
    <td>".$authorname."</td><td>".$posttype."".$forum['subject']."</td><td>".$forum['replies']."</td><td>".$forum['views']."</td>
    </tr>";

我不想张贴整个函数,因为它是相当多的代码,当我不相信这是问题,因为它在第一页上工作,我相信它与我的Jquery有关,但如果你想看看函数本身,它是在github下https://github.com/Doxramos/Invontrol/blob/master/plugins/inv_forum/functions.php线75 - 123我读了委托事件,我相信这是正确的脚本,但我不确定问题是什么,在这一点上。

使用下面的代码

你的问题叫Event Delegation

事件委托允许我们将单个事件侦听器附加到父元素,该元素将为所有匹配选择器中是否存在这些后代,或者是否已添加到未来。

<script>
    jQuery(document).ready(function($) {
       $(document).on('click','.clickable-row',function() {
          window.document.location = $(this).data("href");
       });
    });
</script>

如果每次新页面调用时都调用click event,请使用下面的代码。

<script>
    jQuery(document).ready(function($) {
       $(document).off('click').on('click','.clickable-row',function() {
          window.document.location = $(this).data("href");
       });
    });
</script>

你已经给了类'clickable-row' tr.而不是tr类名,尝试jQuery 'on'方法表类名。例如,将类'clickable-tbl'传递给table标签并修改jQuery代码,如下所示:

$(".clickable-tbl").on('click',$('.clickable-row'),function() {
        window.document.location = $(this).data("href");
    });