【问题标题】:Why click event handler works on the first page only为什么单击事件处理程序仅适用于第一页
【发布时间】:2014-06-02 21:17:06
【问题描述】:

在我的"fnInitComplete" : function(oSettings, json) 中,我有一个像$('[id^=f_]').each(function () 这样的选择器。

Datatables 获取其数据服务器端和"bProcessing":true

我知道我的选择器只能在我的数据表的第一个页面上工作,当我更改页面时,我的功能将无法工作.

但是,即使使用 ajax 异步加载,有什么解决方案可以使这项工作正常进行。我不想放弃ajax。

一个例子:

"fnInitComplete" : function(oSettings, json) {
$('[id^=r_]').each(function () {
    $(this).click(function(e) {
        alert('Foo');
        e.preventDefault();
    })
});
}

仅适用于第一个加载的行数据表。有什么建议吗?

【问题讨论】:

    标签: javascript jquery ajax jquery-selectors datatables


    【解决方案1】:

    使用 on() 将操作绑定到绑定完成后可能不在 DOM 上的元素。

    "fnInitComplete" : function(oSettings, json) {
        $(document).on('click', '[id^=r_]', function () {
            alert('Foo');
            e.preventDefault();
        })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2013-10-02
      相关资源
      最近更新 更多