Get the selected value of the select tag

Get the selected value of the select tag

1.1 Example explanation
<!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" specifies that the option (when first displayed in the list) appears 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 gets the selected value of the select tag
	var obj = document.getElementById("testSelect");
	var index = obj.selectedIndex; //selected index
	var text = obj.options[index].text; //select text
	var val = obj.options[index].value; //selected value

	//Get the selected select value in jQuery
	var text=$('#testSelect option:selected').text(); //selected text
	var val=$('#testSelect option:selected') .val(); //selected value
	var index=$("#testSelect").get(0).selectedIndex; //索引
</script>
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444478&siteId=291194637