2

我在循环内调用了甜蜜警报功能,但甜蜜警报只显示一次,我认为它覆盖了以前的甜蜜警报,bcz 当我执行简单警报时它确实弹出两次,但甜蜜警报只显示我一次时间。我想要做的是当我点击确定或取消按钮时发出甜蜜警报以显示第二个或第三个警报,否则不会根据循环迭代显示第二个或下一个警报这是我的代码

$.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;
      }
    });

}

提前致谢..

4

1 回答 1

2

$.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
        });

    }
于 2015-05-26T06:43:40.570 回答