我使用“关闭事件”来避免多个AJAX请求(例如,如果用户做了多次点击)。完美的作品。jQuery - 关()不依赖于事件
问题是,根据“on('click')”事件的调用方式,事件off()根本不起作用。我举一个例子:
功能:
var addComment(elem) = function(){
$.ajax({
type: "POST",
url: '../../ajax/request.php',
dataType: 'json',
data: data,
beforeSend: function(){
// after this line, click the elem doesn't do anything
elem.off('click');
},
success: function(result){
if (result.success){
console.log('success!');
}else{
console.log('no success...');
}
//making "elem" clickable again
elem.on('click',function(){
addComment(elem);
});
},
error: function(xhr, ajaxOptions, thrownError){
alert("Error Status: " + xhr.status + " Thrown Errors: "+thrownError);
}
});
}
活动:
// Using this, the off event works normally
$('.divArea button.newComment').on('click',function(){
addComment($(this));
});
// Using this, the off event simply is not executed
$('.divArea')
.on('click', 'button.newComment',function(){
addComment($(this));
})
为什么在第二种情况下,关闭()不工作,我应该做到off()在两个事件调用示例中都能正常工作?
谢谢先进。
您的函数定义语法是错误的,该代码如何运行? – Barmar
它应该是'var addComment = function(elem){...}'或'function addComment(elem){...}' – Barmar
在ajax之后单击并重新启用时禁用该按钮会更简单吗?返回? –