防止在弹出窗口外单击时关闭SweetAlert

时间:2017-12-11 08:44:07

标签: javascript sweetalert

我正在使用 Sweet Alert 在电子商务应用程序的产品视图中弹出一个带有两个按钮的弹出窗口:一个用于进入购物车视图,另一个用于重新加载视图。

但是当用户点击弹出窗口外,弹出窗口会自动关闭。 我试过跟随属性来阻止它被关闭但没有任何作用:

hideOnOverlayClick: false,
hideOnContentClick: false,
closeClick: false,
helpers: {
    overlay: { closeClick: false } 
}

非常感谢任何帮助/建议。

感谢。

13 个答案:

答案 0 :(得分:19)

如果您使用的是Sweet Alert 2,则可以使用此配置

allowOutsideClick: false

这应该有用。

答案 1 :(得分:15)

您正在寻找的房产是closeOnClickOutside:

closeOnClickOutside: false

答案 2 :(得分:9)

关于Sweet Alert 2(更有效的解决方案)

实际上,这里的所有答案都没有涵盖另一种消除弹出窗口的方法。那就是使用键盘。特别是 ESC 键。为了防止这种情况,您需要添加两个选项,而不是一个。

allowOutsideClick: false,
allowEscapeKey: false,

快速示例:

Swal.fire({
  title: 'Do not dismiss!',
  icon: 'warning',
  showConfirmButton: false,
  allowOutsideClick: false,
  allowEscapeKey: false
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>

答案 3 :(得分:7)

对于SweetAlert 2

allowOutsideClick: false

和2之前使用的版本

closeOnClickOutside: false

答案 4 :(得分:2)

它是allowOutsideClick:false例如

swal({
  title: "View Cart",
  text: "Are you sure?",
  type: "warning",
  showCancelButton    : true,
  confirmButtonColor  : "#ff0000",
  confirmButtonText   : "Yes",
  allowOutsideClick: false,
  CancelButtonText    : "No"
            },
                function() //confirm
            {
                //if Yes do this
            }
);

答案 5 :(得分:2)

我遇到了同样的问题,这是解决问题的方法:setCanceledOnTouchOutside(false)

var dialog = SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
dialog.setTitleText(getString(R.string.session_expired));
dialog.setConfirmClickListener { sDialog ->
    sDialog.dismissWithAnimation()
    Utils.signOut(context!!)
    Handler().postDelayed({
    startActivity(getLoginIntent(context!!))
    AcTrans.Builder(context!!).performFade()
    }, 500)};
dialog.setCanceledOnTouchOutside(false);
dialog.show();

答案 6 :(得分:1)

backdrop:trueallowOutsideClick: false一起使用,如下所示。它对我有用。

swal({
    backdrop:true,
    allowOutsideClick: false,
    title:'Warning!',
    text:'Do you want to delete records?',
    type:'warning',
    showCancelButton: 0,
    confirmButtonText: 'OK',
}).then(function(e) {
    if (e.value) {
        //do what you want
    }
})

答案 7 :(得分:1)

我们正在使用高于2的版本来进行出汗警报,在我们的情况下,我们需要。

对于Chrome:

closeOnClickOutside: false

对于Firefox:

allowOutsideClick: false 

答案 8 :(得分:0)

您可以设置此属性:

allowOutsideClick: true

答案 9 :(得分:0)

如果您不想关闭esc或外部对话框,请点击以下为我工作。

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this details!",
  icon: "warning",
  closeOnClickOutside: false,
  closeOnEsc: false,
  allowOutsideClick: false,
  buttons: [
    'No, cancel it!',
    'Yes, I am sure!'
  ],
  dangerMode: true,
})

答案 10 :(得分:0)

如果上述答案对您不起作用,请尝试:

closeOnClickOutside: false

答案 11 :(得分:0)

对于Sweetalert版本<2

swal(
         "Records will be deleted permanently.",  //title
         "Do you want to delete records?",  //text
         "warning",  //icon
         {
              closeOnClickOutside: false, // prevent close on click anywhere/outside
              buttons: ["No", "Yes"], //with custom label
              dangerMode: true,
         }
    ).then(ok => {
         if (ok) {
              console.log("deleted")
         }
         else {
              console.log("not deleted")
         }
    })

答案 12 :(得分:0)

对于最新版本是

allowOutsideClick: false