我在循环内调用 Sweetalert 函数,但 sweetalert 只显示一次,我认为它覆盖了之前的 sweetalert,bcz 当我执行简单警报时,它会弹出两次,但 Sweetalert 仅显示一次给我看了一次。 我想要做的是当我单击“确定”或“取消”按钮时,甜蜜警报会显示第二个或第三个警报,否则不会根据循环迭代显示第二个或下一个警报 这是我的代码
$.each(data,function(index,elem){
NotificationPopUpIsApprove();
});
function NotificationPopUpIsApprove(){
swal({
title: "Are you sure?",
text: "The Task "+ task_name + " In Project " + project_name + "is Completed by " + uname ,
type: "warning",
confirmButtonText: "Approve!",
closeOnConfirm: false,
confirmButtonColor: "#44E753",
cancelButtonText: "Reasssign!",
showCancelButton: true,
},
function(isConfirm){
if (isConfirm === true) {
return true;
}else{
return false;
}
});
}
提前致谢..
最佳答案
$.each 是一个同步 jQuery 方法,因此它不会等待触发此行为的 sweetalert 响应。
根据您的要求,希望这对您有所帮助。
var data = ['a', 'b', 'c'];
var task_name = "sweetalert";
var project_name = "Something";
var uname ="SweetAlert"
$(document).ready(function () {
NotificationCheck(data[0]);
});
function NotificationCheck(noti) {
is_last = (data.indexOf(noti) + 1 == data.length);
NotificationPopUpIsApprove(is_last, function (boool) {
if (boool) { // Do something with response from user
console.log(noti);
}
if (data.indexOf(noti) < (data.length)) { // Inject next element for user response
next = data.indexOf(noti) + 1;
NotificationCheck(data[next]);
}
});
}
function NotificationPopUpIsApprove(is_last, callback) {
swal({
title: "Are you sure?",
text: "The Task " + task_name + " In Project " + project_name + "is Completed by " + uname,
type: "warning",
confirmButtonText: "Approve!",
closeOnConfirm: false,
confirmButtonColor: "#44E753",
cancelButtonText: "Reasssign!",
showCancelButton: true,
},
function (isConfirm) {
if (is_last) { //Close SweetAlert box
swal.close();
}
callback(isConfirm); //Sends user's response
});
}
关于javascript - 甜蜜警报正在循环中覆盖先前的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366694/