jQuery select(设置默认选中,获取选中的文本)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
        $("select").val(2);//设置第2节被选中
        $("select").get(0).selectedIndex=2;//下标从0开始,即下标为2选中的是第3个option
	
	//select的change事件
	$("select").change(function(){
            alert($("select").val());//选中的值,对应<option>标签里面的value属性
            alert($("select option:selected").text());//选中的文本值(1)
            alert($("select").find("option:selected").text());//选中的文本值(2)
	})
});
</script>
</head>

<body>
	<select>
		<option value="1">第1节</option>
		<option value="2">第2节</option>
		<option value="3">第3节</option>
	</select>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/ml863606/article/details/88363724