Reputation: 2126
I need help. When I submit this form I want it to automatically redirect to the value option (which is url) in the select list.
So on pressing submit for Ahmedabad selected in select list it should redirect to http://intranet.guj.nic.in/passport
Currently it is not redirecting. What is the best way to do it using inline javascript code?
<html><body>
<form id="pass-form" >
<select id="pass-dropdown" ><option selected="selected">Select Passport Office
for Status page </option><option value="http://intranet.guj.nic.in/passport/">Ahemadabad
</option><option value="http://passportstatus.nic.in/passmain.php?city=asr">Amritsar
</option><option value="http://rpobangalore.gov.in/npassport.asp">Bangalore
</option><option value="http://passportstatus.nic.in/passmain.php?city=bly">Bareilly
</option></select>
<input type="submit" onsubmit="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" />
</form>
</body>
</html>
Upvotes: 2
Views: 36172
Reputation: 368
In some cases you might have to use return
to ensure the desired return value gets used:
<form onsubmit="return redirectMe();">
<input placeholder="Search" type="text">
</form>
... more code here ...
function redirectMe() {
window.location.replace("http://stackoverflow.com");
return false;
}
Source: How to pass an event object to a function in Javascript?
Upvotes: 3
Reputation: 26386
Change the type = "submit"
to button
<input type="button" onclick="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" />
Update
<input type="button" onclick="var sel = document.getElementById('pass-dropdown');
var frm = document.getElementById("pass-form"); frm.action = sel; frm.submit();" />
It would be better if you put that in a function and call the function instead
Upvotes: 1
Reputation: 66663
onsubmit is an event of the <form>
element and not of the submit button.
Change your form code to:
<form id="pass-form" onsubmit="window.location.href = 'newlocation'; return false;">
...
<input type="submit" value="Submit" />
</form>
Upvotes: 8
Reputation: 55
<form id="pass-form" onsubmit="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" >
...
<input type="submit" />
</form>
You must do the submit attribute to the form tag. Then it should work. http://www.w3schools.com/jsref/event_form_onsubmit.asp
Upvotes: 0