24

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

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

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

1 回答 1

101
$('.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...
            }
        });
    }
});
于 2011-06-17T10:11:12.210 回答