SweetAlert2 - 绑定另一个事件取消按钮?

Pie*_*iet 8 button sweetalert sweetalert2

我正在使用最新版本的真棒SweetAlert2 jquery插件.

我有一个带2个按钮的简单SweetAlert.1按钮是确认按钮,另一个是取消按钮.我正在使用html选项向警报添加文本输入.当用户按下确认按钮时,执行AJAX呼叫并关闭警报.

现在我想使用取消按钮执行一些其他代码,而不是关闭警报的默认操作.(用户可以使用showCloseButton:true关闭警报).

所以简而言之:如何删除关闭处理程序并将自己的自定义处理程序添加到swal的取消按钮?

小智 17

只需添加自定义函数即可捕获拒绝,例如:

swal({
   title: 'Some title',
   text: 'some text for the popup',
   type: 'warning',
   showCancelButton: true,
   cancelButtonText: 'Some text for cancel button'
}).then(function(){
   // function when confirm button clicked
}, function(dismiss){
   if(dismiss == 'cancel'){
       // function when cancel button is clicked
   }
});
Run Code Online (Sandbox Code Playgroud)

您甚至可以添加更多功能来捕获另一个解雇事件,只需阅读SweetAlert2 Docs以获取有关解雇事件的更多信息.


小智 10

通过对@Raditya Adi Baskara答案进行一些自定义,

swal({
        title: "$titleWarnignBox",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#36c6d3',
        cancelButtonColor: '#d33',
        confirmButtonText: '$confrimBtn',
        cancelButtonText: '$cancelBtn'
    }).then(function(result){
        if(result.value){
            console.log('good');
        }else if(result.dismiss == 'cancel'){
           console.log('cancel');
        }

    });
Run Code Online (Sandbox Code Playgroud)


Hay*_*ore 5

  1. 您可以创建一个自定义html文件,并在其中具有一个“取消”按钮,这将触发您自己的取消处理程序。

例如

<html> <!--custom.html-->      
  <button id="confirmBtn">confirm<button>
  <button id="cancelBtn">cancel<button>
<html>

$.get("custom.html", function (data) {
        swal({
            html: data,
            showCloseButton: false,
            showCancelButton: false,
            showConfirmButton: false
        });
        $("#cancelBtn").click(function () {
            //handle your cancel button being clicked
            $.when($.ajax({....})).then(function() {
                 // when all AJAX requests are complete
             });
        });
        $("#confirmBtn").click(function () {
            //handle your confirm button being clicked
        });
    });
Run Code Online (Sandbox Code Playgroud)
  1. 您可以只记得取消时的弹出窗口。将此添加到您的swal函数。

    function (dismiss) {
       if (dismiss === 'cancel') {
          swal({..});            
       }
    }
    
    Run Code Online (Sandbox Code Playgroud)

所以全

swal({
   title: 'Swal Title',
   text: 'Your swal text',
   type: 'warning',
   showCancelButton: true,
   cancelButtonText: 'cancel'
}).then(function(){
   //Confirmed
}, function(dismiss){
   if(dismiss == 'cancel'){
      //swal({..}); //un-comment this line to add another sweet alert popup on cancel
   }
});
Run Code Online (Sandbox Code Playgroud)