获取select标签选中值

获取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>

猜你喜欢

转载自wsj123.iteye.com/blog/2398501