admin管理员组文章数量:1332107
I am trying to redirect my page to a different url depending on what the user selects from the drop down list. If the user selects "A", the url is the default URL that's in the action attribute of the form but if the user selects "B", I want to redirect it to a different url.
My problem has been no matter what I do, it always redirects to the default URL(ie oldUrl.do) even if I select "B" from the drop down.
What can I do to point it to the newUrl.do?
Note: I am using Struts 1.2 and testing it on IE8.
My jsp page:
<form name="myForm" method="post" action="oldUrl.do" id="myForm">
<select name="modeOfT" id="choice"><option value="A">A</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<input type="submit" name="submitBtn" onclick="submitForm()">
</form>
Javascript function:
function submitForm(){
var form = document.getElementById("myForm");
var mode = document.getElementById("choice");
if(mode.value == "B"){
$(form).attr("action", "newUrl.do");
}
}
Following are other things I have tried and they still take me to "oldUrl.do":
document.location.href("newUrl.do");
window.location.href="newUrl.do";
top.location.href="newUrl.do";
parent.location.href="newUrl.do";
window.location.replace("newUrl.do");
window.location="newUrl.do";
I am trying to redirect my page to a different url depending on what the user selects from the drop down list. If the user selects "A", the url is the default URL that's in the action attribute of the form but if the user selects "B", I want to redirect it to a different url.
My problem has been no matter what I do, it always redirects to the default URL(ie oldUrl.do) even if I select "B" from the drop down.
What can I do to point it to the newUrl.do?
Note: I am using Struts 1.2 and testing it on IE8.
My jsp page:
<form name="myForm" method="post" action="oldUrl.do" id="myForm">
<select name="modeOfT" id="choice"><option value="A">A</option>
<option value="A">A</option>
<option value="B">B</option>
</select>
<input type="submit" name="submitBtn" onclick="submitForm()">
</form>
Javascript function:
function submitForm(){
var form = document.getElementById("myForm");
var mode = document.getElementById("choice");
if(mode.value == "B"){
$(form).attr("action", "newUrl.do");
}
}
Following are other things I have tried and they still take me to "oldUrl.do":
document.location.href("newUrl.do");
window.location.href="newUrl.do";
top.location.href="newUrl.do";
parent.location.href="newUrl.do";
window.location.replace("newUrl.do");
window.location="newUrl.do";
- 3 Have you tried window.location = "your URL"; – Will Hawker Commented Feb 12, 2013 at 17:42
- yes i have. Thank you. I will add that to my list. – Susie Commented Feb 12, 2013 at 17:43
- 1 Your code seems to work fine jsfiddle/xv3Uf/5 (I still suggest using the submit handler as in my answer) – Ruan Mendes Commented Feb 12, 2013 at 18:11
- @javaStudent Please provide some feedback on what we've told you. I'm curious to find out what wasn't working... – Ruan Mendes Commented Feb 12, 2013 at 18:59
3 Answers
Reset to default 4You should not handle the click event. To deal with a form being submitted you should use the submit event, for this and many other reasons (people can use enter, they can hit space while focus in on the submit button)
Working example http://jsfiddle/xv3Uf/2/
HTML
<form name="myForm" method="post" action="oldUrl.do" id="myForm" onsubmit="return submitForm()" >
JS
function submitForm(){
var form = document.getElementById("myForm");
var mode = document.getElementById("choice");
if(mode.value == "B"){
form.action = "newUrl.do";
}
}
And since you're already using jQuery
, why not drink the kool-aid and stop the pre-enlightenment practice of setting JS handlers in the HTML? http://jsfiddle/xv3Uf/3/
<form name="myForm" method="post" action="oldUrl.do" id="myForm">
JS
$('#myForm').submit(function(e){
if ( $('#choice').val() === 'B') {
this.action = 'newUrl.do'
}
});
You could:
Have the server send back an HTTP/redirect header depending on what the user selects in the form.
If you want the redirect to be handled at the browser, use an "onsubmit" attribute, and return a
false
value. You can handle the redirection within this function usingwindow.location = <URL>;
<form onsubmit="return myJSFunc()" .. > ... <input type="submit"> </form>
Turn the <submit>
into a regular <button>
.
In the submitForm() function, at the end, call:
document.forms["myForm"].submit();
本文标签: javaRedirecting to a new URL using javascriptStack Overflow
版权声明:本文标题:java - Redirecting to a new URL using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235092a2437932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论