简体   繁体   English

对于jquery插件数据表,为什么click事件不起作用?

[英]For the jquery plugin datatables, why the click event not functioning?

I am using datatables, it is easy and powerful tool to generate table using jquery 我正在使用数据表,这是使用jQuery生成表的简单而强大的工具

The reference link is here: You can simply try at that website too. 参考链接在这里:您也可以在该网站上尝试。

https://datatables.net/ https://datatables.net/

The problem is , For example, I have a table with 20 rows. 问题是,例如,我有一个包含20行的表。 If it show 10 row per page, there are two pages. 如果每页显示10行,则有两页。 And for each row , there is a button, and I capture the click event on that button. 并且对于每一行,都有一个按钮,我捕获了该按钮上的click事件。 eg 例如

$('.row_button').on('click',function()){
....
});

The problem is , for the first page buttons the click event can be trigger but for the other page it doesn't , simply nothing response the script has no error in the chrome developer. 问题是,对于第一页按钮,单击事件可以触发,但对于其他页面则不能,单击脚本的任何响应在chrome开发人员中都没有错误。 I think the page navigation button affect the click event . 我认为页面导航按钮会影响click事件。 but How to fix it? 但是如何解决呢? thanks 谢谢

Besides the answer above , in fact the offical has already have this answer 除了上面的答案,实际上官员已经有了这个答案

Most common FAQs 最常见的常见问题

Q. My events don't work on the second page 问:我的活动在第二页上不起作用

A. When attaching events to cells in a table controlled by DataTables, you need to be careful how it is done. 答:将事件附加到由DataTables控制的表中的单元格上时,您需要小心如何完成此操作。 Because DataTables removes nodes from the DOM, events applied with a static event listener might not be able to bind themselves to all nodes in the table. 因为DataTables从DOM中删除了节点,所以使用静态事件侦听器应用的事件可能无法将自身绑定到表中的所有节点。 To overcome this, simply use jQuery delegated event listener options, as shown in this example. 为了克服这个问题,只需使用jQuery委托的事件侦听器选项,如本示例所示。 Additionally, you could use my Visual Event bookmarklet to help debug event issues. 另外,您可以使用Visual Event书签来帮助调试事件问题。

reference: http://datatables.net/faqs/#events 参考: http : //datatables.net/faqs/#events

 $('#example tbody').on('click', 'tr', function () {
        var name = $('td', this).eq(0).text();
        alert( 'You clicked on '+name+'\'s row' );
    } );

elegant and works 优雅与作品

After some searching, I manage to get a ugly solution , it seems the problem is caused by each page is dynamic add to page, however, the jquery "on" is not working and I have no idea about it. 经过一番搜索,我设法得到一个丑陋的解决方案,看来问题是由每个页面都是动态添加到页面引起的,但是,“ on”的jQuery无法正常工作,我对此一无所知。 Finally this code is works. 最终这段代码是可行的。

   $('#data_table').dataTable({
            "fnDrawCallback": function() {
                $('.showDetail').unbind('click');
                $('.showDetail').on('click', function() {
                ...........
                });
            }
        });

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

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