使用jQuery,很容易从下拉列表中选择文本。这是使用选择ID完成的。若要更改下拉列表的选定值,请使用val()方法。
您可以尝试运行以下代码,以了解如何更改下拉列表的选定值。在这里,我们有一个默认的选择选项first,但是警报显示了第二个 选项,已使用val method()对其进行了更改:
<html>
<head>
<title>jQuery Selector</title>
<script src = "https://cdn.staticfile.org/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>