10

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

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

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

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

4

6 回答 6

20

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

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 文档以获取有关解除事件的更多信息。

于 2017-11-07T05:17:49.540 回答
13

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

    });
于 2018-08-07T07:07:17.200 回答
8
  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
   }
});
于 2017-08-14T02:10:56.420 回答
3

在 SweetAlert2 中。

狼火({
        title: '这里的标题',
        显示关闭按钮:假,
        显示取消按钮:真,
        显示确认按钮:假,
        焦点确认:假,
        允许外部点击:假,
        allowEscapeKey:假,
        cancelButtonText: '取消付款'
    }).then(函数(res) {
        如果(res.isDismissed){
            alert('点击取消按钮');
        }
    });
于 2020-08-17T08:35:49.413 回答
2

在甜蜜警报 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**');
                    }
                });
于 2019-08-24T07:53:13.603 回答
1

sweetalert2,如果要防止单击取消按钮时关闭,可以将自己的自定义按钮添加为 html:(实时运行

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>`
});
于 2020-09-02T00:40:35.463 回答