javascript 单击提交按钮后如何重定向用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15220339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:55:13  来源:igfitidea点击:

How do I redirect users after submit button click?

javascript

提问by mplungjan

How do I redirect users after submit button click? My javascript isn't working:

单击提交按钮后如何重定向用户?我的 javascript 不工作:

Javascript

Javascript

<script type="text/javascript" language="javascript">
function redirect()
{
    window.location.href="login.php";
}
</script>

Form Page

表单页面

<form  name="form1" id="form1" method="post">  
    <input type="submit" class="button4" name="order" 
        id="order" value="Place Order" onclick="redirect();" >
</form>

回答by mplungjan

Your submission will cancel the redirect or vice versa.

您的提交将取消重定向,反之亦然。

I do not see the reason for the redirect in the first place since why do you have an order form that does nothing.

我首先没有看到重定向的原因,因为为什么你有一个什么都不做的订单。

That said, here is how to do it. Firstly NEVER put code on the submit button but do it in the onsubmit, secondly return false to stop the submission

也就是说,这是如何做到的。首先永远不要把代码放在提交按钮上,而是在提交时执行,其次返回 false 以停止提交

NOTEThis code will IGNORE the action and ONLY execute the script due to the return false/preventDefault

注意此代码将忽略操作并仅执行脚本,因为返回 false/preventDefault

function redirect() {
  window.location.replace("login.php");
  return false;
}

using

使用

<form name="form1" id="form1" method="post" onsubmit="return redirect()">  
  <input type="submit" class="button4" name="order" id="order" value="Place Order" >
</form>

Or unobtrusively:

或不显眼:

window.onload=function() {
  document.getElementById("form1").onsubmit=function() {
    window.location.replace("login.php");
    return false;
  }
}

using

使用

<form id="form1" method="post">  
  <input type="submit" class="button4" value="Place Order" >
</form>

jQuery:

jQuery:

$("#form1").on("submit",function(e) {
   e.preventDefault(); // cancel submission
   window.location.replace("login.php");
});

-----

-----

Example:

例子:

$("#form1").on("submit", function(e) {
  e.preventDefault(); // cancel submission
  alert("this could redirect to login.php"); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>


<form id="form1" method="post" action="javascript:alert('Action!!!')">
  <input type="submit" class="button4" value="Place Order">
</form>

回答by Roger

Using jquery you can do it this way

使用 jquery 你可以这样做

$("#order").click(function(e){
    e.preventDefault();
    window.location="login.php";
});

Also in HMTL you can do it this way

同样在 HMTL 你可以这样做

<form name="frm" action="login.php" method="POST">
...
</form>

Hope this helps

希望这可以帮助

回答by Chris

Why don't you use plain html?

你为什么不使用纯html?

<form action="login.php" method="post" name="form1" id="form1">
...
</form>

In your login.php you can then use the header()function.

在您的 login.php 中,您可以使用header()函数。

header("Location: welcome.php");

回答by masterofdestiny

use

利用

window.location.replace("login.php");

or simply window.location("login.php");

或者 simply window.location("login.php");

It is better than using window.location.href =,because replace()does not put the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.

它比使用更好,window.location.href =,因为replace()不会将原始页面放入会话历史记录中,这意味着用户不会陷入永无止境的后退按钮惨败中。如果要模拟某人单击链接,请使用location.href. 如果要模拟 HTTP 重定向,请使用location.replace.

回答by Tapas Pal

It would be

这将是

window.location="login.php";

回答by vvvvvvvvvv

I hope this might be helpful

我希望这可能会有所帮助

<script type="text/javascript">
 function redirect() {
  document.getElementById("formid").submit();
 }
 window.onload = redirect;
</script>
<form id="formid" method="post" action="anypage.jsp">
  .........
</form>

回答by Rubyist

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com/SpecificAction.php");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com/SpecificAction.php";