当甜蜜警报关闭时如何倾听

时间:2017-10-09 05:42:29

标签: javascript jquery sweetalert sweetalert2

我目前正在使用sweetalert2,我正在尝试检测警报何时关闭。但是,DeleteUnsavedImages函数未触发。我认为将函数分配给onclose键会起作用,但没有运气。

   swal({
       html: data,
       showCloseButton: false,
       showCancelButton: false,
       width: 800,
       showConfirmButton: false,
       onClose: DeleteUnsavedImages()
   }).then(function () {

   });


function DeleteUnsavedImages(){
    var test = "-1";
}

任何帮助将不胜感激: - )

3 个答案:

答案 0 :(得分:4)

我测试了我的甜蜜警报以确认问题,你只需要传递函数名称而不需要(),函数将在swal的onClose事件处理程序中调用。它被称为传递函数的引用,以便在onClose被触发swal时调用。

像这样做一点改变:

   swal({
       html: data,
       showCloseButton: false,
       showCancelButton: false,
       width: 800,
       showConfirmButton: false,
       onClose: DeleteUnsavedImages        // Removed () from here
   }).then(function () {

   });


   function DeleteUnsavedImages(){
       var test = "-1";
   }

答案 1 :(得分:0)

swal({
     html: data,
     showCloseButton: false,
     showCancelButton: false,
     width: 800,
     showConfirmButton: false,
     onClose: () => {
         DeleteUnsavedImages();
     }
})

答案 2 :(得分:0)

swal({
    title: "client",
    content: html,
    buttons:
    {
        cancel: {
            text: "Close",
            visible: true,
            closeModal: true,
        },
        confirm: {
            text: "Download",
            visible: true,
            closeModal: false
        }
    },
}).then((confirm) => {
    if (confirm) {
        download();
    }
    else {
        DeleteUnsavedImages();
    }
});

function DeleteUnsavedImages(){
    var test = "-1";
}