Javascript 需要帮助在提交时重定向表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14170996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:54:29  来源:igfitidea点击:

Need help to redirect the form on submit

javascripthtmldrupal

提问by AgA

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.

我需要帮助。当我提交此表单时,我希望它自动重定向到选择列表中的值选项(即 url)。

So on pressing submit for Ahmedabad selected in select list it should redirect to http://intranet.guj.nic.in/passport

因此,在按下选择列表中选择的艾哈迈达巴德的提交时,它应该重定向到http://intranet.guj.nic.in/passport

Currently it is not redirecting. What is the best way to do it using inline javascript code?

目前它没有重定向。使用内联 javascript 代码的最佳方法是什么?

<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>

回答by techfoobar

onsubmitis an event of the <form>element and not of the submit button.

onsubmit<form>元素的事件,而不是提交按钮的事件。

Change your form code to:

将您的表单代码更改为:

<form id="pass-form" onsubmit="window.location.href = 'newlocation'; return false;">
...
    <input type="submit" value="Submit" />
</form>

回答by apebeast

In some cases you might have to use returnto ensure the desired return value gets used:

在某些情况下,您可能必须使用return以确保使用所需的返回值:

<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?

来源:如何将事件对象传递给 Javascript 中的函数?

回答by codingbiz

Change the type = "submit"to button

更改type = "submit"为按钮

<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

如果你把它放在一个函数中并调用该函数会更好

回答by ijo

<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

您必须对表单标记执行提交属性。那么它应该工作。 http://www.w3schools.com/jsref/event_form_onsubmit.asp