开发者

Intercepting a jQuery.ajax() call with confirm()

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜