使用confirm()拦截jQuery.ajax()调用

Fau*_*ust 21 jquery confirm

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

有没有办法让确认在同步上下文中工作?

HTML:

<a href="#" class="removeItem delete">remove</a>
Run Code Online (Sandbox Code Playgroud)

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...
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Гро*_*ный 91

$('.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...
            }
        });
    }
});
Run Code Online (Sandbox Code Playgroud)