加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

JavaScript中取得Select元素的当前选项实例化方法的示例代码

发布时间:2023-08-28 14:49:06 所属栏目:教程 来源:
导读:JavaScript获取Select当前值写法:

var value = document.getElementById("select").options[document.getElementById("select").options.selectedIndex].value;

var text = document.getElementById("selec
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()"/>
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章