使用window.location.replace重定向不起作用

Redirecting using window.location.replace not working

本文关键字:重定向 不起作用 replace location window 使用      更新时间:2023-09-26

无论出于何种原因,在我按下提交后,我的代码都不会重定向到另一个文件。

      $('form').submit(function () {
      window.location.replace('test.html');  });

我已经在一个html文件中定义了所有内容。有什么想法吗?

谢谢,busterroni

您需要停止提交表单。这可以通过对传入的事件对象调用preventDefault()来完成

$('form').submit(function (e) {
    e.preventDefault();
    window.location.replace('test.html');  
});

您没有取消表单提交,而是创建了一个竞赛条件。您已将表单提交回当前页面,并且您的位置正试图导航离开。

如果您想让后退按钮工作,您可能希望使用assign()而不是replace()