簡體   English   中英

需要幫助才能在提交時重定向表單

[英]Need help to redirect the form on submit

我需要幫助。 當我提交此表單時,我希望它自動重定向到選擇列表中的值選項(即url)。

所以當按下選擇列表中選擇的艾哈邁達巴德提交時,它應該重定向到http://intranet.guj.nic.in/passport

目前它沒有重定向。 使用內聯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>

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

將您的表單代碼更改為:

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

在某些情況下,您可能必須使用return來確保使用所需的返回值:

<form onsubmit="return redirectMe();">
  <input placeholder="Search" type="text">
</form>

... more code here ...

function redirectMe() {
  window.location.replace("http://stackoverflow.com");
  return false;
}

來源: 如何在Javascript中將事件對象傳遞給函數?

type = "submit"更改為按鈕

<input type="button" onclick="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" />

更新

<input type="button" onclick="var sel = document.getElementById('pass-dropdown'); 
       var frm = document.getElementById("pass-form"); frm.action = sel; frm.submit();" />

如果你把它放在一個函數中並調用函數會更好

<form  id="pass-form" onsubmit="var sel = document.getElementById('pass-dropdown'); window.location = sel.options[sel.selectedIndex].value" >
    ...
    <input type="submit" />
</form>

您必須對表單標記執行submit屬性。 然后它應該工作。 http://www.w3schools.com/jsref/event_form_onsubmit.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM