首页 > 解决方案 > sweetalert 关闭后的重定向

问题描述

在 sweetalert 模式超时关闭后,我想被重定向到另一个页面。我正在使用以下代码。模式正在显示,它在计时器结束后关闭,但新页面没有被重定向。

$('#with-timer').on('click', function () {
    var timerInterval;
    swal({
      title: 'Auto close alert!',
      html: 'I will close in <strong>2</strong> seconds.',
      timer: 2000
    }).then(function (result) {
      if (result.dismiss === swal.DismissReason.timer) {
        window.location.href = "index.php";
      }
    });
  });

有什么建议可以解决这个问题吗?

谢谢你。

标签: javascriptphphtmljquery

解决方案


看起来您正在将 sweetalert 与 sweetalert2 混合使用。它们有不同的 API。看起来 sweetalert2 告诉您警报被解除的原因,所以我将使用它来回答这个问题,尽管它用于swal.fire打开警报。

这是一个工作示例:

$('#with-timer').on('click', function () {
    var timerInterval;
    swal.fire({
      title: 'Auto close alert!',
      html: 'I will close in <strong>2</strong> seconds.',
      timer: 2000
    }).then(function (result) {
      if (result.dismiss === swal.DismissReason.timer) {
        window.location.href = "https://stackoverflow.com";
      }
    });
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<button id="with-timer">Open Modal</button>


推荐阅读