使用 confirm() 拦截 jQuery.ajax() 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6384251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Intercepting a jQuery.ajax() call with confirm()
提问by Faust
I have an ajax call bound to a link via jQuery, and I want it intercepted by a confirm dialog. But the ajax call fires regardless of which option is selected (even if the user just closes the dialog).
我有一个通过 jQuery 绑定到链接的 ajax 调用,我希望它被确认对话框拦截。但是无论选择哪个选项,ajax 调用都会触发(即使用户只是关闭对话框)。
Is there a way to get the confirm to work as it does in synchronous contexts?
有没有办法让确认像在同步上下文中一样工作?
HTML:
HTML:
<a href="#" class="removeItem delete">remove</a>
jQuery:
jQuery:
$('.delete').click(function () {
confirm('Are you sure you want to delete this?');
});
$('.removeItem').click(function (event) {
event.preventDefault();
$.ajax({
url: 'myUrl',
type: "POST",
data: {
// data stuff here
},
success: function () {
// does some stuff here...
}
});
});
回答by Грозный
$('.removeItem').click(function (event) {
if (confirm('Are you sure you want to delete this?')) {
$.ajax({
url: 'myUrl',
type: "POST",
data: {
// data stuff here
},
success: function () {
// does some stuff here...
}
});
}
});