0
点赞
收藏
分享

微信扫一扫

JavaScript获取select值,文本


HTML CODE:


<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn").click(function(){
//原生JS获取
var t = document.getElementById("select");
alert("js value:" + t.options[t.selectedIndex].value); //获取value
alert("js text:" + t.options[t.selectedIndex].text); //获取text

//jQuery获取
alert("jquery val:"+$("#select").val());//选中项值-方式一
//alert("jquery val:"+$("#select option:selected").val());//选中项值-方式二
alert("jquery text:"+$("#select option:selected").text());//选中项文本
//alert("jquery index:"+$("#select option:selected").index());//选中项索引

});});
</script>
</head>
<body>
<select id="select">
<option value="1">第一项</option>
<option value="2">第二项</option>
<option value="3">第三项</option>
</select>
<br/><br/>
<div id="btn" style="font-size:13px;color:red;cursor:pointer;">点击我读取下拉框选中信息</div>
</body>
</html>

​​在线演示​​  将以上代码复制粘贴到代码块中,点击提交代码,即可查询演示结果

举报

相关推荐

0 条评论