jQuery off 似乎不适用于数据表中的图像
jQuery off seems not working for images inside a dataTable
我有一个数据表,每一行都有一个弹出确认消息的可点击图标,如果点击确认按钮,下次点击该图标时,不会出现任何消息。由于以编程方式填充行,因此每个图标都具有相同的 ID 名称和 class。代码如下
$('#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() 方法,但在单击一个图标后,这使得 table 中的所有图标都没有响应。我该如何解决这个问题?
不确定 $(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!');
},
}
});
});
我有一个数据表,每一行都有一个弹出确认消息的可点击图标,如果点击确认按钮,下次点击该图标时,不会出现任何消息。由于以编程方式填充行,因此每个图标都具有相同的 ID 名称和 class。代码如下
$('#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() 方法,但在单击一个图标后,这使得 table 中的所有图标都没有响应。我该如何解决这个问题?
不确定 $(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!');
},
}
});
});