首页 > 语言 > JavaScript > 正文

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

2024-05-06 14:31:48
字体:
来源:转载
供稿:网友

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()"/>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选