我想在 html select 上选择一个选项。例如,选择:
<select id="list">
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
</select>
如果我知道选项的值,我可以这样做,例如:
<option value="0">Jan</option>
$("#list option:eq(0)").attr("selected", "selected");
我可以使用选项标记之间的值吗?
最佳答案
使用.val(value)
,在示例中我使用 Mar
设置为值
Set the value of each element in the set of matched elements.
$("#list").val('Mar')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="list">
<option>Jan</option>
<option>Feb</option>
<option>Mar</option>
<option>Apr</option>
</select>
关于jquery - 以编程方式选择 html 选择元素选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21536976/