我在html中有一个包含下拉菜单的表单
<form name= "gadget_selector"> <select name= "gadget"> <option value = 0> Something </option> <option value = 1> Something else </option> //etc.. </select> </form>
我想在javascript函数中访问所选选项的值,如此
function someFunction(){ //var option = value of selected menu option }
我该怎么办?
解决方法
var option = document.getElementById('gadget').value;
将小工具设置为选择的ID,如下所示:
<select id="gadget" name="gadget"> <option value = 0> Something </option> <option value = 1> Something else </option> //etc.. </select>