javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)

  2020-05-13 我要评论

JavaScript获取Select当前值写法:
var value = document.getElementById("select").options[document.getElementById("select").options.selectedIndex].value;
var text = document.getElementById("select").options[document.getElementById("select").options.selectedIndex].text;

例子:

复制代码 代码如下:

<script>
function check() {
      var select = document.getElementById("select").options[document.getElementById("select").options.selectedIndex].value;
      alert(select);
}
</script>

<select name="select" id="select">
<option value="test1" selected="selected">Test1</option>
<option value="test2">Test2</option>
</select>
<input type="button" value="我要试试" onclick="check()"/>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们