zl程序教程

您现在的位置是:首页 >  前端

当前栏目

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

JavaScriptChrome代码 获取 示例 当前 兼容 IE
2023-06-13 09:15:14 时间

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

例子:

复制代码代码如下:

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

<selectname="select"id="select">
<optionvalue="test1"selected="selected">Test1</option>
<optionvalue="test2">Test2</option>
</select>
<inputtype="button"value="我要试试"onclick="check()"/>