javascript 下拉菜单 onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13932093/
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
javascript dropdown menu onclick
提问by CHarris
I've been working at this for a while now, but don't understand what's wrong with my code. I'm sure it's something simple - it always is!
我已经在这方面工作了一段时间,但不明白我的代码有什么问题。我相信这很简单 - 它总是如此!
Basically, I have a drop down menu, with some options. I want it to go to a web page when the third option, plumber, is selected. When any of the others are clicked, nothing should happen.
基本上,我有一个下拉菜单,有一些选项。我希望它在选择第三个选项管道工时转到网页。当任何其他人被点击时,什么都不应该发生。
My code so far is:
到目前为止我的代码是:
<select id = 'search'>
<option value="1">Carpenter</option>
<option value="2">Electrician</option>
<option value="3">Plumber</option>
<option value="4">Tiler</option>
<option value="5">Boiler Installation</option>
</select>
Go
走
And my javascript is:
我的 javascript 是:
<script>
function go_button {
if (search.value=="3") {
location="search_results.htm"
}
}
</script>???????
But it's not working. Could someone tell me what's wrong?
但它不起作用。有人能告诉我出了什么问题吗?
Thanks.
谢谢。
C.
C。
采纳答案by t0.
You can either put a change eventon your control via script or add it directly to your control..
您可以通过脚本在控件上放置更改事件,也可以将其直接添加到控件中。
Direct Method:
直接法:
<select id="search" onChange="OnSelectedIndexChange()">
This is the function you need to put in your Script:
这是您需要放入脚本的函数:
//function for changed selection
function OnSelectedIndexChange()
{
if (document.getElementById('search').value == "3"){
location.href="search_results.htm";
}
}
Add Change event using Script (either JavaScript or JQuery):
使用脚本(JavaScript 或 JQuery)添加 Change 事件:
I suggest JQueryfor doing so (the onSelectedIndexChange function is obsolete here)
我建议使用JQuery(这里的 onSelectedIndexChange 函数已过时)
$('#search').change( function() {
if(this.val() == "3"){
location.href="search_results.htm";
}
});
If you don't want to use JQuery just add the following code:
如果您不想使用 JQuery,只需添加以下代码:
var yourdropdown = document.getElementById('search');
yourdropdown.Attributes.Add("onChange", "return OnSelectedIndexChange();")
回答by huncyrus
I think - if possible - use jQuery or other js framework, both have built-in select and other html/dom plugins & methods.
我认为 - 如果可能的话 - 使用 jQuery 或其他 js 框架,两者都有内置的 select 和其他 html/dom 插件和方法。
回答by Jon Taylor
You need to tell javascript which element it is you are interested in.
你需要告诉 javascript 你对哪个元素感兴趣。
Use getElementById("search")to return the element then you can look into getting its value.
使用getElementById("search")返回元素,那么你可以考虑获取其值。
回答by s-m-e
<select id="search">
I think, there are to many spaces.
我认为,有很多空间。
getElementById("search").value
I have not used this in a while, but is not it ...
我已经有一段时间没有使用这个了,但不是......
location.href = "search_results.htm";
Somewhat like this gives you access to the object. And last but not least, you need to associate your function "go_button" with an event of the drop down box, something like this:
有点像这样,您可以访问该对象。最后但并非最不重要的一点是,您需要将函数“go_button”与下拉框的事件相关联,如下所示:
<select id="search" onClick="go_button();">

