用confirm()截取jQuery.ajax()调用 [英] Intercepting a jQuery.ajax() call with confirm()

查看:256
本文介绍了用confirm()截取jQuery.ajax()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过jQuery绑定到链接的ajax调用,并且我希望它被确认对话框拦截.但是无论选择哪个选项,都会触发ajax调用(即使用户只是关闭对话框也是如此).

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).

是否有一种方法可以使确认像在同步上下文中一样工作?

Is there a way to get the confirm to work as it does in synchronous contexts?

HTML:

<a href="#" class="removeItem delete">remove</a>

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...
        }
    });
});

推荐答案

$('.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...
            }
        });
    }
});

这篇关于用confirm()截取jQuery.ajax()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆