简体   繁体   English

jQuery .on('click')在DataTables第2页或11以上的行中不起作用

[英]JQuery .on('click') not working in DataTables 2nd page or rows past 11

I have a jQuery link that runs on a dynamic list for each row when the hyperlink is clicked. 当单击超链接时,我有一个jQuery链接在每行的动态列表上运行。

This works before datatables is applied, but once datatables is applied the 11th row (after changing display to higher than the default 10) or when on another page, the jQuery is no longer called. 这在应用数据表之前有效,但是一旦应用了数据表,则在第11行(将显示更改为高于默认值10后)或在另一页上时,不再调用jQuery。

I tried throwing this in a jsFiddle and it works there, so I can't reproduce it in a jsFiddle for some reason. 我尝试将其扔到jsFiddle中,并且在那里工作,因此由于某种原因我无法在jsFiddle中重现它。

Any pointers in the right direction would be very much appreciated. 朝正确方向的任何指针将不胜感激。

PHP: PHP:

echo "<table id='paginatedTable'>";
echo "<thead><th>Test1</th><th>Test2</th></thead><tbody>";
foreach($array as $arr){
 echo "<tr><td>" . $arr['test1'] . "</td><td><div class='test'>";
 echo "<a href='#' class='toggleTest' data-id='". $arr['id']."' id='test-" . $arr['id'] . "'>" . $arr['test2'] . "</a>";
 echo "</div></td></tr>";
}
echo "</tbody></table>";

jQuery jQuery的

$(function(){
    $('.test').on('click', '.toggleTest', function(e){
        var id = $(this).data('id');
        $("#test-"+id).html("Done");
        return false;
    });
});

$(document).ready(function() {
    $('#paginatedTable').dataTable();
} );

You need to bind the handler to a static element, not the rows that can be added dynamically. 您需要将处理程序绑定到静态元素,而不是可以动态添加的行。 So it should be: 因此应该是:

$("#paginatedTable").on("click", ".test .toggleTest", function ...);

I have followed the reply from @Barmar. 我一直关注@Barmar的回复。

along with that, i have wrapped the on click into the document.ready function, then it worked for me. 随之而来的是,我将单击包装到document.ready函数中,然后对我有用。

$(document).ready(function() {

$('#dmtable tbody').on( 'click', 'tr td.details-control', function () {

}
} );

Another easy solution I just found is to just add a function that uses a component handler to upgrade the DOM on any DataTables redraw. 我刚刚发现的另一个简单解决方案是仅添加一个使用组件处理程序的函数,以在任何重绘的DataTables上升级DOM。

Can be used like: 可以像这样使用:

$(document).ready(function(){
    var table = $('#table-1').DataTable({ 
        "fnDrawCallback": function( oSettings ) {
            componentHandler.upgradeDOM();
        }
    });
});

It solved my pagination issues when I was using a dropdown in one of the columns. 当我在其中一列中使用下拉菜单时,它解决了我的分页问题。

There are a few things that can go wrong here: 这里有些事情可能会出错:

  • Your event binding is on elements that get replaced, you should use something like: 您的事件绑定位于要替换的元素上,您应该使用类似以下内容的东西:
    $('#paginatedTable').on('click', '.toggleTest', function(e){
  • You don't seem to be escaping your html so data could possibly break it: 您似乎并未转义html,因此数据可能会破坏它:
    htmlspecialchars($arr['test2'])
    (instead of just $arr['test2'] , could apply to other variables as well) (而不只是$arr['test2'] ,也可以应用于其他变量)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM