`
wsj123
  • 浏览: 148726 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

获取select标签选中值

    博客分类:
  • JS
阅读更多
获取select标签选中值

1.1实例讲解
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Examples</title>
</head>
<body>
<select name="testSelect" id="testSelect">
	<option value="1">1</option>
	<option value="2">2</option>
	<!-- selected="selected",规定选项(在首次显示在列表中时)表现为选中状态。 -->
	<option value="3" selected="selected">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
</select>
<!--[if !IE]><!-->  
    <script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>  
<!--<![endif]-->  
<!--[if lte IE 9]>  
    <script src="https://cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>  
<![endif]--> 
<script>
	//Js获取select标签选中值
	var obj = document.getElementById("testSelect");
	var index = obj.selectedIndex; //选中索引
	var text = obj.options[index].text; //选中文本
	var val = obj.options[index].value; //选中值

	//jQuery中获得选中select值
	var text=$('#testSelect option:selected').text(); //选中的文本
	var val=$('#testSelect option:selected') .val(); //选中的值
	var index=$("#testSelect").get(0).selectedIndex; //索引
</script>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics