虽然您不能设置 Sweet Alerts 块,但您可以使用它的回调功能来触发任何需要先解除警报的代码。这些例子有几个例子。例如,假设您有一个是/否样式警报,则有文件删除示例。
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
//The callback will only fire on cancel if the callback function accepts an
//argument. So, if the first line were 'function () {' with no argument, clicking
//cancel would not fire the callback.
if (isConfirm) {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
或者 AJAX 示例:
swal({
title: "Ajax request example",
text: "Submit to run ajax request",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function(){
setTimeout(function(){
swal("Ajax request finished!");
}, 2000);
});
在这两种情况下,直到与警报交互时才会触发回调,并且调用的结果作为参数传递给回调。
所以说你需要等到有人点击确定。
swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue",
closeOnConfirm: false
}, function () {
swal("Deleted account", "We'll miss you!", "success");
});
注意:仅当您在回调中显示后续警报时,才需要使用closeOnConfirm
/ 。如果设置为,它将在向用户显示之前关闭第二个警报。但是,如果您正在做一些不相关的事情,并且您没有关闭它,它将无限期地保持打开状态。closeOnCancel
false
true
swal
swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue"
}, function () {
console.log("This still shows!")
});
如果您希望警报在您执行不swal
相关的操作时保持打开状态,您应该swal.close()
在代码末尾调用。
swal({
title: "Delete your account?",
text: "Clicking on continue will permanently delete your account.",
type: "warning",
confirmButtonText: "Continue",
closeOnConfirm: false
}, function () {
console.log("This still shows!");
setTimeout(function () {
// This will close the alert 500ms after the callback fires.
swal.close();
}, 500);
});