在用户单击“确定”之前,甜蜜警报会重定向

时间:2016-11-16 09:42:15

标签: javascript sweetalert

我正在使用此页面上的SweetAlert2和第7个示例:

Go to 7th example below to see it in action

这一个:

    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this imaginary file!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    },
    function() {
        swal(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        );
    });
像这样:

function deleteEvent(id)
{
    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this event!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    }).then(function() {
        swal(
          'Deleted!',
          'Your event has been deleted.',
          'success'
        );
            $.ajax({
                url: '/events/'+id,
                type: "DELETE",
                data:{ _token: "{{csrf_token()}}" },
                success: function() {
                    location.href = '/events';
                }
            });
    });
}

但是当用户点击确认按钮删除....这个弹出窗口 enter image description here

由于我的重定向,

马上出现,用户甚至没有机会点击确定按钮......

如何在用户点击此图片上的“确定”后进行重定向?

1 个答案:

答案 0 :(得分:5)

我终于解决了这个问题:

function deleteEvent(id)
{
    swal({
        title: 'Are you sure?',
        text: 'You will not be able to recover this event!',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!',
        closeOnConfirm: false
    }).then(function() {

        $.ajax({
            url: '/events/'+id,
            type: "DELETE",
            data:{ _token: "{{csrf_token()}}" }
        }).done(function() {

            swal({
                title: "Deleted", 
                text: "Event has been successfully deleted", 
                type: "success"
            }).then(function() {
                location.href = '/events';
            });
        });
    });
}