Javascript - 表单提交后重定向到主页

时间:2016-05-24 22:13:26

标签: javascript html forms

我有一张表格:

  <form name="orderform" onsubmit="submitForm()">
 HTML HERE
.
.
.
<button type="submit" name="submitbutton"   id="submitbutton">Submit</button>

和功能:

function submitForm()
{
 submitted = true;
 goHome(submitted);
}

function goHome(submitted)
{
  if(redirect == submitted)
{
  alert("form submitted");
  document.location.href = "index.html";
}
}

我试图让我的页面显示一条警告消息,然后在提交表单后重定向到index.html,但我似乎无法以任何方式使其正确。 我尝试在提交按钮上使用onClick或仅使用Javascript(document["orderform].submit();document.getElementById("orderform");)提交表单,但确实无效。

我的猜测是这些功能在提交的同时执行,导致提交执行。因此,我为setTimeout添加了function goHome();以执行或尝试window.onload = goHome();但似乎没有任何效果。

有什么建议吗?

PS:我没有使用jQuery。

2 个答案:

答案 0 :(得分:0)

由于您使用的是标准HTML FORM提交流程,因此旧页面将被销毁并加载新页面。

有两种解决方案:

  • 最好的方法:让您提交表单的任何页面在其标题中发出HTTP重定向。

  • 不太理想的方式:通过Ajax提交表单,然后在成功时重新执行JavaScript。

答案 1 :(得分:0)

设置html文件头部的相应部分以包含

<meta http-equiv="REFRESH" content="3;url=index.html"> <!-- 3 seconds to reload here -->
在你的goHome函数中

相关问题