我有一个dataTable,在每一行中都有一个可单击的图标,弹出一个确认消息,如果单击了确认按钮,则下次单击该图标时,不会出现任何消​​息。当以编程方式填充行时,每个图标都具有相同的ID名称和类。代码如下所示

$('#myTable tbody').on( 'click', 'img.sc', function () {
    $.confirm({
    title: 'Confirm!',
    content: 'You will report an issue..',
    buttons: {
    confirm: function () {
             $(this).off('click' );
             //other stuff ..}
    cancel: function () {
             $.alert('Canceled!');
                      },
             }
     });
});


该代码无效,每次单击该图标都会显示该消息。我也尝试使用one()方法,但是在单击图标后,这使表中的所有图标都没有响应。我怎样才能解决这个问题?

最佳答案

不知道$(this)是否引用这里的元素。请尝试以下操作:

    $('#myTable tbody').on('click', 'img.sc', function() {
        let elem = $(this);
        $.confirm({
            title: 'Confirm!',
            content: 'You will report an issue..',
            buttons: {
                confirm: function() {
                    elem.off('click');
                    //other stuff ..
                },
                cancel: function() {
                    $.alert('Canceled!');
                },
            }
        });
    });

关于javascript - jQuery关闭似乎不适用于数据表中的图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60033821/

10-12 12:53
查看更多