两种方式,原生js和jQuery。
使用原生js获取select标签选中值,源码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="#">
<select id="test">
<option value="1">VsCode</option>
<option value="2">Atorm</option>
</select>
<input type="button" value="测试" onclick="t1()">
</form>
<script>
function t1(){
var myselect=document.getElementById("test");
var index=myselect.selectedIndex;
alert(myselect.options[index].value)
}
</script>
</body>
</html>
使用jQuery获取select标签选中值,源码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="#">
<select id="test">
<option value="1">VsCode</option>
<option value="2">Atorm</option>
</select>
<input type="button" value="测试" onclick="t1()">
</form>
<script src="jquery-1.8.0.min.js"></script>
<script>
function t1(){
var options=$("#test option:selected");
alert(options.val());//获取value
alert(options.text());//获取文本
}
</script>
</body>
</html>