【问题标题】:Intercepting a jQuery.ajax() call with confirm()使用 confirm() 拦截 jQuery.ajax() 调用
【发布时间】:2011-06-17 10:08:07
【问题描述】:

我有一个通过 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...
        }
    });
});

【问题讨论】:

    标签: jquery confirm


    【解决方案1】:
    $('.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...
                }
            });
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-18
      • 2011-07-13
      • 1970-01-01
      • 2018-07-08
      • 2017-07-19
      • 2014-10-11
      • 2011-05-14
      相关资源
      最近更新 更多