如何使用 jQuery 从下拉列表(选择框)中获取选定的文本?
jqueryweb developmentfront end technology更新于 2026/2/10 10:37:17
使用 jQuery,可以轻松地通过 :selected 从下拉列表中获取选定的文本。这可以通过选择 ID 来完成。您可以尝试运行以下代码,了解如何使用 jQuery − 从下拉列表中获取选定的文本。
示例
<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() {
$("#submit").click(function(){
$("#myselection option:selected").text();
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>
</select>
<button id="submit">Result</button>
</div>
</body>
</html>

