我使用以下内容作为删除按钮,尽管我收到了确认消息,但如果我单击“取消”,它仍然会继续执行:
<a onclick="return confirm('are you sure?')" class="btn btn-default btn-sm deleteList" href="#" list_id="1467" title="Delete your list: listName">Delete</a>
有什么明显的吗?
更新:
它附加到 jquery click 函数,因此如果取消停止链接,我如何防止点击处理程序触发?
更新2
也在点击处理程序开始时尝试过,但同样的问题:
$('.deleteList').on('click', function(e) {
confirm('are you sure?');
var list_id = $(this).attr("list_id");
$.ajax({
context: this,
type: "POST",
url: '/ajax/actions/deleteList.php?listId=' + list_id,
success: function (data) {
var d = $.now();
window.location.href = "/pages/dashboard?listDelete=" + d + "#mylists";
}
}); // End .ajax
e.preventDefault();
});
最佳答案
这是万无一失的答案:
$('.deleteList').on('click', function( event ) {
event.preventDefault()
event.stopImmediatePropagation(); //de Added by @crush
return confirm("are you sure?")
})
然后删除onclick="..."
。
截至更新的问题,整个问题是不同的。现在的问题是 confirm()
的无效使用以及 $.ajax()
可能出现的异步问题。
关于javascript - 返回确认仍在执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21092616/