使用 jQuery sweetalert2 时出错

Error when use jQuery sweetalert2

这是我在添加 sweetalert2 和删除 post 之前的代码:

if (action == "delete") {
            this.model.destroy({
                beforeSend: function() {
                    target.addClass('loading');
                    view.blockUi.block(view.$el);
                },
                success: function(result, status, jqXHR) {
                    view.blockUi.unblock();
                    target.removeClass('loading');
                    if (status.success) {

                        if (result.get('post_type') == "post")
                            window.location.href = status.redirect;
                        else
                            view.$el.fadeOut();
                    } else {
                        // Error
                    }
                }
            });
            return false;
        }

这是我使 sweetalert2 与动作兼容的编辑:

if (action == "delete") {

            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then(function () {
                swal(
                    'Deleted!',
                    'Your post has been deleted.',
                    'success'
                ),
                  this.model.destroy({
                    beforeSend: function() {
                        target.addClass('loading');
                        view.blockUi.block(view.$el);

                    },

                        success: function(result, status, jqXHR) {
                            view.blockUi.unblock();
                            target.removeClass('loading');
                            if (status.success) {

                                if (result.get('post_type') == "post")
                                    window.location.href = status.redirect;
                                else
                                    view.$el.fadeOut();
                            } else {
                                // Error
                            }

                    }
                })
            });
            return false;
        }

我找不到错误,sweetalert2 对话框工作正常但删除操作 post 不工作,我该怎么办?

I can't find the mistake the sweetalert2 dialog working right but the action of delete post not working, What can I do?

当您最初调用 sweetalert 时,它会提示用户做出响应。

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

如果用户确认,那么你就可以执行代码了。您已经实施了一种捕获成功和错误的方法,因此当其中任何一种发生时,您只需再次调用 sweetalert 以覆盖前一个并向用户显示正确的警报。您可以选择执行相同的操作,因为如果用户决定取消以向他们提供更多反馈。

我相信这会成功:

if (action == "delete") {

swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
}).then(function () {

      this.model.destroy({
        beforeSend: function() {
            target.addClass('loading');
            view.blockUi.block(view.$el);

        },
            success: function(result, status, jqXHR) {
                view.blockUi.unblock();
                target.removeClass('loading');
                if (status.success) {
                    // Success
                    swal(
                        'Deleted!',
                        'Your file has been deleted.',
                        'success'
                      )
                } else {
                    // Error
                    swal(
                      'Failed',
                      'Your file has not been deleted',
                      'error'
                    )
                }

        }
    })
}, function () {
// Cancelled
    swal(
      'Cancelled',
      'Your file has not been deleted',
      'error'
    )
});

return false;
}