Toggle not working with DataTable responsive

旧时模样 提交于 2019-12-12 06:21:16

问题


This is how I am coding:

$(document).ready(function () {

    $("[id^=btnToggle]").click(function () {
        $('#infoToggle' + this.id.match(/\d+$/)[0]).toggle();
    });

    $('#dataTables-example').DataTable({
        responsive: true
    });

});

Here this code $("[id^=btnToggle]") is not working, but when I remove $('#dataTables-example') it is working fine.

I tried reversing their positions, I tried using separate document ready function, it did not work.

Please let me know how to fix this?

Thanks


回答1:


The dom(#dataTables-example) is modified when after calling the datatable event. due to which inner elements no more have previously attached events .You need to use event delegation in this case:

 $("body").on('click','[id^=btnToggle]',function () {
    $('#infoToggle' + this.id.match(/\d+$/)[0]).toggle();
});


来源:https://stackoverflow.com/questions/32293221/toggle-not-working-with-datatable-responsive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!