如何解决从sweetalert2获取结果时遇到问题
function confirmation() {
swal.fire({
title: "Are you sure?",text: "Your about to delete some files",type: "warning",showCancelButton: true,showConfirmButton: true,confirmButtonText: 'Yes,I am sure!',cancelButtonText: "No,cancel it!",cloSEOnConfirm: false,cloSEOnCancel: false
}).then((result) => {
if (result.isConfirmed) {
swal.fire("Deleted!","files are successfully deleted!","success");
} else {
window.location = "....";
}
});
}
但即使我确认或否认它也会取消我能做什么?
我也尝试过使用 isDenied
,但仍然没有。
解决方法
对于 SweetAlert 中的取消按钮,您应该使用 SweeetAlertResult 对象的 .isDismissed()
方法。您还使用了不推荐使用的属性(closeOnConfirm、CloseOnCancel、type):
function confirmation() {
swal.fire({
title: "Are you sure?",text: "Your about to delete some files",icon: 'success',showCancelButton: true,showConfirmButton: true,confirmButtonText: 'Yes,I am sure!',cancelButtonText: "No,cancel it!",}).then((result) => {
if (result.isConfirmed) {
swal.fire("Deleted!","files are successfully deleted!","success");
console.log("Delete files...")
}
if (result.isDismissed) {
swal.fire("Cancelled!","Files not deleted!","warning");
console.log("Doing Nothing")
}
});
}
JSFiddle:https://jsfiddle.net/sdurwc9y/
点击取消按钮等同于在SweetAlert 模式窗口外点击。所以你可以使用 else
而不是 if (result.isDismissed)
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。