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

SweetAlert2 - Bind another event to cancel button?

我正在使用最新版本的很棒的 SweetAlert2 jquery 插件。

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

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

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

  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
        });
    });
  1. 您可以回忆一下取消时的弹出窗口。 将此添加到您的 swal 函数中。

    function (dismiss) {
       if (dismiss === 'cancel') {
          swal({..});            
       }
    }
    

所以完整

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

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

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

您甚至可以添加更多函数来捕获另一个关闭事件,只需阅读 SweetAlert2 Docs 了解有关关闭事件的更多信息。

对@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');
        }

    });

在 sweetalert 2

            swal.fire({
                title: "Notice",
                text: "Are you sure ?",
                showCancelButton: true,
                type: 'error',
                cancelButtonColor: '#d33',
            })
                .then((res) => {
                    if(res.value){
                        console.log('confirmed');
                    }else if(res.dismiss == 'cancel'){
                        console.log('cancel');
                    }
                    else if(res.dismiss == 'esc'){
                        console.log('cancle-esc**strong text**');
                    }
                });

在 SweetAlert2 中。

Swal.fire({
        title: 'Title here',
        showCloseButton: false,
        showCancelButton: true,
        showConfirmButton: false,
        focusConfirm: false,
        allowOutsideClick: false,
        allowEscapeKey: false,
        cancelButtonText: 'Cancel Payment'
    }).then(function(res) {
        if (res.isDismissed) {
            alert('Cancel button clicked');
        }
    });

sweetalert2中,如果您想防止点击取消按钮时关闭,您可以添加自己的自定义按钮,如html: (Run it live)

var onBtnClicked = (btnId) => {
  // Swal.close();
  alert("you choosed: " + btnId);
};
Swal.fire({
  title: "What you want to do?",
  icon: "warning",
  showConfirmButton: false,
  showCloseButton: true,
  html: `
     <p>select an action</p>
    <div>
      <button class="btn btn-primary" onclick="onBtnClicked('reply')">Reply</button>
      <button class="btn btn-danger" onclick="onBtnClicked('delete')">Delete</button>
    </div>`
});