我想知道是否有可能让jQuery在下拉框中选择一个<option>
,比如第4项?
<select>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
我希望用户单击一个链接,然后让<select>
框更改它的值,就像用户通过单击<option>
选择了它一样。
发布于 2011-02-01 23:46:08
解决方案:
$("#element-id").val('the value of the option');
发布于 2013-01-17 22:25:17
HTML select元素有一个selectedIndex
属性,可以写入该属性以选择特定的选项:
$('select').prop('selectedIndex', 3); // select 4th option
使用普通JavaScript可以通过以下方式实现:
// use first select element
var el = document.getElementsByTagName('select')[0];
// assuming el is not null, select 4th option
el.selectedIndex = 3;
发布于 2015-11-05 18:22:00
我会这样做的
$("#idElement").val('optionValue').trigger('change');
https://stackoverflow.com/questions/4864620
复制相似问题