javascript 基于下拉选择的调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18137825/
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
Call function based on dropdown selection
提问by Lemonio
I have the following dropdown
我有以下下拉菜单
<select onchange="myFunc(argument here)" id="propertydropdown">
<option value="tags">blah</option>
<option value="tags2">blahblah</option>
<option value="tags3">blahblahblah</option>
</select>
how do i get the argument to be the value of the option currently selected?
我如何让参数成为当前选择的选项的值?
回答by Bergi
回答by SimonR
I'm not sure how you could pass the selected value as an argument, but what you could do is within the handler itself use this
to work out the value (this
will be the select):
我不确定如何将选定的值作为参数传递,但您可以做的是在处理程序本身内用于this
计算值(this
将是选择):
function myFunc() {
var value = this.options[ this.selectedIndex ].value;
// ...do stuff
}
You might even be able to do onclick="myFunc( this.options[ this.selectedIndex ].value );"
, but I haven't tried that.
你甚至可以这样做onclick="myFunc( this.options[ this.selectedIndex ].value );"
,但我还没有尝试过。