微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么SweetAlert的显示格式不正确?

如何解决为什么SweetAlert的显示格式不正确?

下面是我的AJAX调用之一的代码。当进入else条件时,SweetAlert代码不会以该代码格式触发。我不知道为什么当我使用此代码时,它可以正常工作:

swal("Task is still in progress","Wait for status change to Done","warning");    

由于我要重定向到单击swal上的OK的页面,因此无法使用此代码。 可能是什么原因?

if (placeholder4Status == 'Done') {
  $.ajax({
    type: "GET",url: "/addTasks-approve",dataType: "json",data: {
      text: text,id: id
    },contentType: "application/json",success: function(response) {
      if (response.status == '200') {
        window.location.replace('/tasksAdmin');
        $.LoadingOverlay("hide");
      }
    }
  });
} else {
  $.LoadingOverlay("hide");
  // swal("Task is still in progress","Wait for status change to Done ","warning");
  swal({
    title: "Task is still in progress!",text: "Please wait for status change to Done ",type: "warning",showCancelButton: true
  },function() {
    window.location.href = '/tasksAdmin';
    // window.location.replace('/tasksAdmin');
  });

  return false;
}

解决方法

放置执行window.location.href的函数并不十分正确。相反,您可以尝试使用Promises更改代码,这是一个示例:

swal({
  title: "Task is still in progress!",text: "Please wait for status change to Done ",type: "warning",showCancelButton: true
}).then((result) => {
  if (result) {
     window.location.href = '/tasksAdmin';
  }
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。