js获取select下拉框选中的值
[ 2016/11/07, JavaScript , 4372阅, 0评 ]

现在有一个id为test的下拉框,怎么拿到选中的那个值呢?

<select id="test">
  <option value="1">text1</option>
  <option value="2">text2</option>
</select>

分别使用javascript原生的方法和jquery方法

一:javascript原生的方法

var myselect=document.getElementById("test");//拿到select对象
var index=myselect.selectedIndex ;//拿到选中项的索引, selectedIndex代表的是你所选中项的index
myselect.options[index].value;//拿到选中项options的value
myselect.options[index].text;//拿到选中项options的text

二:jquery方法(前提是已经加载了jquery库)

var options=$("#test option:selected");//获取选中的项
alert(options.val());//拿到选中项的值
alert(options.text());//拿到选中项的文本

有朋自远方来...评论一下呗O(∩_∩)O