简体   繁体   English

动态事件绑定未触发

[英]Dynamic event binding not firing

I want to bind a tr click event to my datatable. 我想将tr click事件绑定到我的数据表。

This is my code now: 现在是我的代码:

$(myDataTable).find("tbody tr").on("click", function (e) {
//do the magic
});

It works for the first page. 它适用于第一页。 On second page i got no click event. 在第二页上,我没有点击事件。

.live and .delegate also not work. .live和.delegate也无效。

Anyone with a solution and can say why this is not working? 有解决方案的人,可以说为什么这行不通吗? I would prefer not to use the dataTables render callbacks. 我宁愿不使用dataTables渲染回调。

To bind to dynamic elements you must do this 要绑定到动态元素,您必须执行此操作

$(myDataTable).on("click","tbody tr", function (e) {
//do the magic
});

.on( events [, selector ] [, data ], handler(eventObject) ) .on(事件[,选择器] [,数据],handler(eventObject))

Documentation 文档

To work with dynamic element use the following code 要使用动态元素,请使用以下代码

$(myDataTable).on("click","tbody tr", function (e) {
//do the magic
});

Documentation : http://api.jquery.com/on/ 文档:http://api.jquery.com/on/

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

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