我想显示 Sweet Alert 2提交表单后向用户确认消息如下:
<form id="myForm" data-flag="0" action="" method="POST">
@csrf
<input type="checkbox" id="btn-submit" name="wallet_checked" onchange="this.form.submit();">
</form>
如您所见,我使用了 onchange="this.form.submit();"
来提交表单,而没有使用提交按钮,并且工作正常。
然后我尝试添加此脚本作为向用户显示 SweetAlert 消息的脚本:
$(document).on('click', '#btn-submit', function(e) {
e.preventDefault();
let form = $(this).parents('form');
swal(
{
title: "Attention!",
text: "Are you sure you want to make this",
type: "warning",
allowEscapeKey: false,
allowOutsideClick: false,
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
showLoaderOnConfirm: true,
closeOnConfirm: false
},
function (isConfirm) {
if (isConfirm) {
console.log("YES");
}
return false;
});
});
因此,单击复选框后,该函数会在屏幕上显示 Sweet Alert 消息,但是当我按下 Yes 按钮时,它不会运行 console.log("YES");
.
这意味着 function (isConfirm) {
处的 if (isConfirm) {
无法正常工作。
那么如何解决这个问题呢? isConfirm
有什么问题吗?
最佳答案
将其更改为:
$(document).on('click', '#btn-submit', function(e) {
e.preventDefault();
let form = $(this).parents('form');
swal(
{
title: "Attention!",
text: "Are you sure you want to make this",
type: "warning",
allowEscapeKey: false,
allowOutsideClick: false,
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
showLoaderOnConfirm: true,
closeOnConfirm: false
})
.then((isConfirm) => {
if (isConfirm) {
console.log("YES");
}
return false;
});
});
关于javascript - SweetAlert isConfirm 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68878812/