如何使用 jQuery 获取下拉列表中的选定值?
jqueryweb developmentfront end technology更新于 2026/1/28 5:52:17
使用 jQuery,可以轻松从下拉列表中获取选定的文本。这可以通过使用 select id 来完成。要更改下拉列表中的选定值,请使用 val() 方法。
示例
您可以尝试运行以下代码来了解如何更改下拉列表中的选定值。这里,我们有一个默认的选择选项 first,但警报显示 second 选项,该选项已使用 val method() 进行了更改:
<html>
<head>
<title>jQuery Selector</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button1").click(function(){
$("#myselection").val('2');
alert( $("#myselection option:selected").text() );
});
});
</script>
</head>
<body>
<div>
<p>The selected value:</p>
<select id="myselection">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
</div>
<button id="button1">Click</button>
</body>
</html>

