简体   繁体   English

使用jQuery sweetalert2时出错

[英]Error when use jQuery sweetalert2

This is my code before add sweetalert2 to delete posts: 这是我的代码,然后添加sweetalert2删除帖子:

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;
        }

this is my edit to make sweetalert2 compatible with the action: 这是我的修改,以使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;
        }

I can't find the mistake the sweetalert2 dialog working right but the action of delete post not working, What can I do? 我找不到swealalert2对话框正常运行的错误,但是删除帖子的操作不起作用,该怎么办?

I can't find the mistake the sweetalert2 dialog working right but the action of delete post not working, What can I do? 我找不到swealalert2对话框正常运行的错误,但是删除帖子的操作不起作用,该怎么办?

When you initially call sweetalert it prompts for a response from the user. 最初调用sweetalert时,它会提示用户做出响应。

The then() method returns a Promise. then()方法返回一个Promise。 It takes up to two arguments: callback functions for the success and failure cases of the Promise. 它最多包含两个参数:Promise成功和失败案例的回调函数。

If the user confirms, then you can execute the code. 如果用户确认,则可以执行代码。 You already implemented a way to catch success and error, so when either of those happen, you just need to call sweetalert again to over ride the previous and display the correct alert to the user. 您已经实现了一种捕捉成功和错误的方法,因此,当发生任何一种成功和错误时,您只需再次调用sweetalert即可克服之前的错误并向用户显示正确的警报。 You can do the same, optionally, for if the user decides to cancel to give them more feedback. 如果用户决定取消以提供更多反馈,则可以选择执行相同的操作。

I believe that this would do the trick: 我相信这可以达到目的:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM