单击按钮转到不同的网址
我需要创建 html 代码,如果用户从下拉列表中选择一个项目并单击按钮,那么它将转到相应的链接。
<select name="myList">
<option value="Google" />Google
<option value="yahoo" />yahoo
<option value="Ask" />Ask
</select>
如果用户从下拉列表中选择 Google 并单击按钮,则页面应重定向至 www.google.com,如果选择 yahoo 并单击按钮,则应重定向至 www.yahoo.com< /strong>
我想提一下,在这种情况下我需要按钮。 我不想通过下拉选择进入相应的网站。仅在单击按钮后。
先感谢您。
i need to create html code, in which if user selects an item from drop down list and click button then it will go to respective link.
<select name="myList">
<option value="Google" />Google
<option value="yahoo" />yahoo
<option value="Ask" />Ask
</select>
if user selects Google from drop down list and clicks button, then page should be redirected to www.google.com and if it selects yahoo and clicks button should go to www.yahoo.com
I would like to mention that I need The button in this scenario.
I don't want to go respective site on drop down selection.Only after clicking the button.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HTML:
JavaScript:
您还可以通过执行以下操作在新窗口中打开网站:
顺便说一句,您的选项标签格式不正确。您永远不应该使用 <... />当标签包含内容时的快捷方式。
HTML:
JavaScript:
You could also open the site in a new window by doing this:
As a side note, your option tags are not properly formatted. You should never use the <... /> shortcut when a tag contains content.
首先,将选项值更改为您想要定位的 URL:
接下来,向 select 添加一个 ID,以便您可以定位它:
最后,向 select 控件添加一个 onclick 方法来处理重定向:
First, change the option values to the URL you want to target:
Next, add an ID to the select so you can target it:
Finally, add an onclick method to the select control to handle the redirect:
使用 JavaScript,您可以将
onclick
事件附加到按钮。在事件处理程序中,您可以使用if
语句来检查在select
列表中选择了哪个值,并使用window.location
进行重定向用户访问适当的 URL。我建议将
option
元素的value
更改为所需的 URL,然后您可以简单地获取该值并转到该 URL,而不必检查哪个选项被选中。Using JavaScript, you can attach an
onclick
event to the button. In the event handler, you can use anif
statement to check which value was selected in theselect
list, and usewindow.location
to redirect the user to the appropriate URL.I would suggest changing the
value
of theoption
elements to be the required URL, and you can then simply grab the value and go to that URL, instead of having to check which option was selected.