雖然你不能有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);
});
把一切都放在回調中。我不知道只有原生功能可以阻止。有趣。 +1的詳細答案,儘管從文檔複製 – amlwwalker
@amlwwalker是的。我試圖在我的回答中包括文檔的相關部分,以及最後的一些自定義示例。 :)還有一個名爲[wait.for](https://github.com/luciotato/waitfor)的庫,儘管我對它不熟悉,所以我不能說它在你的用例中的有效性。 –