admin管理员组文章数量:1410682
I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.
Form:
<form id="myform" method="POST">
<a href='' onclick='submitResponse()'>SUBMIT</a>
</form>
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 = "" ;
document.forms[0].submit();
}
I'm trying to submit the form and redirect to href url specified in anchor tag href attribute.
Form:
<form id="myform" method="POST">
<a href='http://www.google.' onclick='submitResponse()'>SUBMIT</a>
</form>
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." ;
document.forms[0].submit();
}
Share
Improve this question
asked Sep 3, 2015 at 11:09
RamRam
1,8033 gold badges22 silver badges41 bronze badges
4 Answers
Reset to default 2You must to read about html forms. To put an action for the form use action attribute
<form action="http://google." 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." ;
document.forms[0].submit();
}
Read up on forms here:
forms
The soloution to your problem:
<form action="http://google." method="POST">
When you execute the form submitt and if you dont specify an url adress in the action the page will simply reload.
the only reason you get redirected is because of this:
window.location.href = "http://www.google." ;
EDIT:
consider this:
function submitResponse() {
$('form').submit(function(){
$.post('http://google.', function() {
window.location = 'http://google.';
});
});
}
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.
Simply update the action attribute of the form tag:
var submitResponse = function(url) {
var form = document.getElementById('myform');
form.action = url;
form.submit();
};
<form id="myform" method="POST">
<a href='http://www.google.' onclick='submitResponse(this.href);return false;'>SUBMIT</a>
</form>
If you are submitting to a URL you can simply return the HTTP response as below.
HTTP/1.1 302 Found
Location: http://www.google.
References:
http://www.w3schools./tags/ref_httpmessages.asp
https://en.wikipedia/wiki/HTTP_302
Update
If you are using
ASP.Net MVC
http://www.dotnet-tricks./Tutorial/mvc/4XDc110313-return-View()-vs-return-RedirectToAction()-vs-return-Redirect()-vs-return-RedirectToRoute().html
http://stackoverflow./questions/7892094/mvc-redirect-to-index-from-another-controller
PHP
http://stackoverflow./questions/768431/how-to-make-a-redirect-in-php
JAVA
http://stackoverflow./questions/2659000/java-how-to-find-the-redirected-url-of-a-url
http://stackoverflow./questions/15057329/how-to-get-redirected-url-and-content-using-httpurlconnection
本文标签: javascriptHTML anchor tag onclick to Submit form and Redirect to href linkStack Overflow
版权声明:本文标题:javascript - HTML anchor tag onclick to Submit form and Redirect to href link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744941892a2633537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论