jQuery off似乎不适用于数据表中的图像

问题描述 投票:0回答:1

我有一个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()方法,但是在单击图标后,这使表中的所有图标均无响应。我该如何解决?

javascript jquery datatable confirm
1个回答
1
投票

不确定$(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!');
                },
            }
        });
    });
© www.soinside.com 2019 - 2024. All rights reserved.