【问题标题】:How to use the sweet alert 2 jquery plugin while trigerring the AJAX calls?如何在触发 AJAX 调用时使用 sweet alert 2 jquery 插件?
【发布时间】:2021-02-16 06:27:33
【问题描述】:

html 页面上有一个取消按钮。当我单击该按钮时,会弹出带有确定和取消选项的警报以及右上角的十字按钮。因此,当我单击 Ok 按钮时,会触发 AJAX 事件,但问题是当我单击取消或单击交叉按钮时,还会触发 AJAX 事件的警报,而这是不应该的。 代码如下:

我不想在单击取消或十字按钮时调用 AJAX。 我该怎么办?

 $("#cancelButton").click(function(){
      Swal.fire({
      title: "We are sad to see you go! ????",
      text: "Do you really want to unsubscribe?",
      icon: "warning",
      showCloseButton: true,
      button: "Ok",
      showCancelButton: true
      }).then((result) => {
        $.LoadingOverlay("show");
          $.ajax({
            type: "GET",
            url: "/v1/subscriptions/{{user.subsID}}",
            dataType: "json",
            contentType: "application/json",
            success: function(response){
              if (response.status == '200') {
                $.LoadingOverlay("hide");
                Swal.fire({
                title: "Cancelled",
                text: "You have been unsubscribed and will no longer hear from us after the end of the current month subscription! ☹️",
                icon: "success",
                button: "Ok",
                showCancelButton: true
                }).then((result) => {
                if (result) {
                window.location.reload();
                }
              });
          }
          else if (response.status == '500') {
            $.LoadingOverlay("hide");
            Swal.fire({
            title: "Error",
            text: "Subscription has already been cancelled",
            icon: "error",
            button: "Ok",
            showCancelButton: true
          })
          }
        }
          });
            })
    });

【问题讨论】:

    标签: javascript jquery ajax sweetalert2


    【解决方案1】:
    Swal.fire({
        title: 'A cool title',
        icon: 'info',
    }).then((result) => {
        if (result['isConfirmed']){
            // Put your function here
        }
    })
    

    这应该可以,result 变量中有一个isConfirmed 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2021-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多