JS获取select信息

这是一个select下拉列表

 <body>
    <select id="test" >
        <option value="1">first</option>
        <option value="2">second</option>
    </select>
</body>

使用JS获取select信息

  • 获取下拉框当前选中的值
var value = document.getElementById("test").value;
  • 获取下拉框当前选中的索引
var index = document.getElementById("test").selectedIndex;
  • 获取select 所有的 option节点对象
//得到的是对象数组
var options = document.getElementById("test").options;
  • 获得第一个option的value
var firstOption = document.getElementById("test").options[0].value;
  • 获得第一个option的文本内容
var firstText = document.getElementById("test").options[0].text;

注意:

没有直接获取当前选中的下拉列表的文本的方法,要获取文本内容,只能通过索引获得。

原文地址:https://www.cnblogs.com/VitoYi/p/7533209.html