Does anyone know how to retrieve the option chosen from a drop-down select box?
有谁知道如何检索从下拉选择框中选择的选项?
I'm using document.getElementById("mySelect").value and it doesn't seem to be working...
我正在使用document.getElementById(“mySelect”)。value并且它似乎不起作用......
3 个解决方案
#1
document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].value
#2
Are you sure your drop-down has a value in it? Did you provide values for it?
你确定你的下拉有价值吗?你为它提供了价值吗?
<select id="dropdownBox">
<option value="val1">value 1</option>
<option value="val2">value 2</option>
<option value="val3">value 3</option>
<option value="val4">value 4</option>
</select>
The values are val1, val2...
值为val1,val2 ......
#3
It's better for code optimization to do something like:
代码优化最好做以下事情:
var o = document.getElementById("mySelect");
o.options[o.selectedIndex].value;
jQuery alternative:
$("#mySelect").val();
#1
document.getElementById("mySelect").options[document.getElementById("mySelect").selectedIndex].value
#2
Are you sure your drop-down has a value in it? Did you provide values for it?
你确定你的下拉有价值吗?你为它提供了价值吗?
<select id="dropdownBox">
<option value="val1">value 1</option>
<option value="val2">value 2</option>
<option value="val3">value 3</option>
<option value="val4">value 4</option>
</select>
The values are val1, val2...
值为val1,val2 ......
#3
It's better for code optimization to do something like:
代码优化最好做以下事情:
var o = document.getElementById("mySelect");
o.options[o.selectedIndex].value;
jQuery alternative:
$("#mySelect").val();