Twitter BootStrap确认不适用于动态生成的元素

shy*_*.me 6 ajax jquery confirmation document-ready twitter-bootstrap

我正在使用DataTable与页面加载时生成的动态内容.在表中我使用了bootstrap确认.在脚本下面加载它.

$( document ).ajaxStop(function() {
    $(document).find('[data-toggle="confirmation"]').confirmation();
});
Run Code Online (Sandbox Code Playgroud)

它打开确认框,但是当点击"是"或"否"时,它不起作用.

这不起作用

我有以下代码来检测'确认'事件.

$(document).ready(function(){
    $(document).find('.delete-record').on('confirmed.bs.confirmation', function() {
        var thisEl = $(this);
        deleteForm($(this).attr('data-delid'));
    });
});
Run Code Online (Sandbox Code Playgroud)

这很有效

$(document).ajaxStop(function(){
    $(document).find('.delete-record').on('confirmed.bs.confirmation', function() {
        var thisEl = $(this);
        deleteForm($(this).attr('data-delid'));
    });
});
Run Code Online (Sandbox Code Playgroud)

怎么了document.ready

在此输入图像描述

编辑:

document.ready在其他页面上使用相同的代码,但没有DataTable,它是HTML DIV结构.

小智 9

尝试稍微更改事件绑定,以便使用备用$.on语法绑定每个现有和将来的.delete-record元素.

$(document).ready(function(){
    $('body').on('confirmed.bs.confirmation', '.delete-record', function() {
        deleteForm($(this).attr('data-delid'));
    });
});
Run Code Online (Sandbox Code Playgroud)

我不知道你的页面结构我选择绑定body,但你可以将它绑定到表的任何父元素.