CHa*_*ris 5 javascript drop-down-menu
我已经为此工作了一段时间,但不明白我的代码有什么问题。我确信这很简单 - 总是如此!
\n\n基本上,我有一个下拉菜单,其中有一些选项。我希望它在选择第三个选项“管道工”时转到网页。单击其他任何一个时,不应发生任何事情。
\n\n到目前为止我的代码是:
\n\n<select id = \'search\'>\n<option value="1">Carpenter</option>\n<option value="2">Electrician</option>\n<option value="3">Plumber</option>\n<option value="4">Tiler</option>\n<option value="5">Boiler Installation</option>\n\n</select>\nRun Code Online (Sandbox Code Playgroud)\n\n去
\n\n我的 JavaScript 是:
\n\n<script>\nfunction go_button {\nif (search.value=="3") {\nlocation="search_results.htm"\n}\n}\n\n</script>\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\xe2\x80\x8b\nRun Code Online (Sandbox Code Playgroud)\n\n但它不起作用。有人能告诉我出了什么问题吗?
\n\n谢谢。
\n\nC。
\n您可以通过脚本在控件上放置更改事件,也可以将其直接添加到控件中。
直接法:
<select id="search" onChange="OnSelectedIndexChange()">
Run Code Online (Sandbox Code Playgroud)
这是您需要放入脚本中的函数:
//function for changed selection
function OnSelectedIndexChange()
{
if (document.getElementById('search').value == "3"){
location.href="search_results.htm";
}
}
Run Code Online (Sandbox Code Playgroud)
使用脚本(JavaScript 或 JQuery)添加 Change 事件:
我建议使用JQuery这样做(onSelectedIndexChange 函数在这里已过时)
$('#search').change( function() {
if(this.val() == "3"){
location.href="search_results.htm";
}
});
Run Code Online (Sandbox Code Playgroud)
如果你不想使用 JQuery 只需添加以下代码:
var yourdropdown = document.getElementById('search');
yourdropdown.Attributes.Add("onChange", "return OnSelectedIndexChange();")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38489 次 |
| 最近记录: |