javascript HTML 锚标记 onclick to Submit form and Redirect to href 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32373978/
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
HTML anchor tag onclick to Submit form and Redirect to href link
提问by Ram
I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.
我正在尝试提交表单并重定向到锚标记 href 属性中指定的 href url。
Form:
形式:
<form id="myform" method="POST">
<a href='http://www.google.com' onclick='submitResponse()'>SUBMIT</a>
</form>
JavaScript:
JavaScript:
function submitResponse() {
document.forms[0].submit();
}
But it only redirects to the link and form is not getting submitted.
但它只重定向到链接,表单没有被提交。
I also tried this and is not working either.
我也试过这个,但也不起作用。
<form id="myform" method="POST">
<a href='#' onclick='submitResponse()'>SUBMIT</a>
</form>
function submitResponse() {
window.location.href = "http://www.google.com" ;
document.forms[0].submit();
}
回答by Marcos Pérez Gude
You must to read about html forms. To put an action for the form use action attribute
您必须阅读有关 html 表单的信息。为表单放置一个动作,使用动作属性
<form action="http://google.com" method="POST">
So your code looks like this
所以你的代码看起来像这样
<form id="myform" method="POST">
<a href='#' onclick='submitResponse()'>SUBMIT</a>
</form>
function submitResponse() {
document.forms[0].action = "http://www.google.com" ;
document.forms[0].submit();
}
回答by ThunD3eR
Read up on forms here:
在这里阅读表格:
The soloution to your problem:
您的问题的解决方案:
<form action="http://google.com" method="POST">
When you execute the form submitt and if you dont specify an url adress in the action the page will simply reload.
当您执行表单提交时,如果您没有在操作中指定 url 地址,页面将简单地重新加载。
the only reason you get redirected is because of this:
您被重定向的唯一原因是:
window.location.href = "http://www.google.com" ;
EDIT:
编辑:
consider this:
考虑一下:
function submitResponse() {
$('form').submit(function(){
$.post('http://google.com', function() {
window.location = 'http://google.com';
});
});
}
The form gets submitted and in the success function you redirect to the desiered site.
表单被提交,并在成功函数中重定向到所需的站点。
the reason your code does not work is since the redirect does not wait for the form to submitt, it directs right away.
您的代码不起作用的原因是因为重定向不等待表单提交,它立即定向。
回答by ThunD3eR
Simply update the action attribute of the form tag:
只需更新表单标签的 action 属性:
var submitResponse = function(url) {
var form = document.getElementById('myform');
form.action = url;
form.submit();
};
<form id="myform" method="POST">
<a href='http://www.google.com' onclick='submitResponse(this.href);return false;'>SUBMIT</a>
</form>
回答by Kishore Sahasranaman
If you are submitting to a URL you can simply return the HTTP response as below.
如果您提交到一个 URL,您可以简单地返回 HTTP 响应,如下所示。
HTTP/1.1 302 Found
Location: http://www.google.com
References:
参考:
http://www.w3schools.com/tags/ref_httpmessages.asp
https://en.wikipedia.org/wiki/HTTP_302
Update
更新
If you are using
如果您正在使用
ASP.Net MVC
ASP.NET MVC
http://www.dotnet-tricks.com/Tutorial/mvc/4XDc110313-return-View()-vs-return-RedirectToAction()-vs-return-Redirect()-vs-return-RedirectToRoute().html
http://stackoverflow.com/questions/7892094/mvc-redirect-to-index-from-another-controller
PHP
PHP
http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php
JAVA
爪哇
http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url
http://stackoverflow.com/questions/15057329/how-to-get-redirected-url-and-content-using-httpurlconnection