Html 如何在没有 GO 按钮的情况下创建下拉列表超链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1599140/
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
How to Create a Dropdown List Hyperlink without the GO button?
提问by Jonathan
I just want to create a dropdown list whenever I choose a new value inside it will take me to a new webpage. I don't want to let user to click "GO" button to go to the page. Just on select and call the action. How can I do such ?
我只想创建一个下拉列表,每当我在其中选择一个新值时,它将带我到一个新网页。我不想让用户点击“GO”按钮进入页面。只需选择并调用操作即可。我怎么能这样做?
<form>
<p align="center"><b>Select a Site </b>
<select id="setit" style="color: #0000FF" size="1" name="test">
<option value="">Select one</option>
<option value="http://www.altavista.com">AltaVista</option>
<option value="http://www.yahoo.com">Yahoo</option>
<option value="http://www.google.com">Google</option></select>
<input type="button" value="Go"
onclick="window.open(setit.options[setit.selectedIndex].value)">
</p></form>
Here for example, this will have the GO button and need to click the GO in order to go to the new page. I don't want the GO button.
例如,这将有 GO 按钮,需要单击 GO 才能转到新页面。我不想要 GO 按钮。
Any ideas?
有任何想法吗?
回答by jerjer
use onchange event:
使用 onchange 事件:
<select onchange="window.open(this.value,'','');">
....
</select>
回答by cleg
As fas as I understood - you simply have to handle onchange event for select tag. Just move your onclick handler there.
正如我所理解的 - 您只需要处理 select 标签的 onchange 事件。只需将您的 onclick 处理程序移到那里即可。
回答by Cody
Put for your onclick
把你的点击
<input type="button" value="Go "onclick="document.location = this.value">
I use this one a lot and should work well
我经常使用这个,应该很好用
(Everyone else seams to be giving you onchange and say don't use javascript, but if the browser a person is using doesn't support javascript then tough shit because there not using chrome or another leading browser)
(其他人似乎都在给你 onchange 并说不要使用 javascript,但是如果一个人使用的浏览器不支持 javascript,那么糟糕,因为没有使用 chrome 或其他领先的浏览器)