简体   繁体   中英

Need help to redirect the form on submit

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>

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>

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?

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM