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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:50:46  来源:igfitidea点击:

Call function based on dropdown selection

javascripthtmlfunctionarguments

提问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

Simply myFunc(this.value), since this== document.getElementByID("propertydropdow")in the handler

很简单myFunc(this.value),因为this==document.getElementByID("propertydropdow")在处理程序中

回答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 thisto work out the value (thiswill 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 );",但我还没有尝试过。