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



>我有一个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!');
},
}
});
});

最新更新