Javascript 获取下拉值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4029281/
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-08-23 07:11:55 来源:igfitidea点击:
Get drop down value
提问by anonymous
How to determine what is selected in the drop down? In Javascript.
如何确定下拉列表中选择的内容?在 JavaScript 中。
回答by cambraca
If your dropdown is something like this:
如果你的下拉菜单是这样的:
<select id="thedropdown">
<option value="1">one</option>
<option value="2">two</option>
</select>
Then you would use something like:
然后你会使用类似的东西:
var a = document.getElementById("thedropdown");
alert(a.options[a.selectedIndex].value);
But a library like jQuery simplifies things:
但是像 jQuery 这样的库可以简化事情:
alert($('#thedropdown').val());
回答by casablanca
Use the value
property of the <select>
element. For example:
使用元素的value
属性<select>
。例如:
var value = document.getElementById('your_select_id').value;
alert(value);
回答by Thomas F.
<select onchange = "selectChanged(this.value)">
<item value = "1">one</item>
<item value = "2">two</item>
</select>
and then the javascript...
然后是javascript...
function selectChanged(newvalue) {
alert("you chose: " + newvalue);
}
回答by Soufiane Hassou
var dd = document.getElementById("dropdownID");
var selectedItem = dd.options[dd.selectedIndex].value;
回答by Pablo Santa Cruz
Like this:
像这样:
$dd = document.getElementById("yourselectelementid");
$so = $dd.options[$dd.selectedIndex];