如何使用jQuery选择下拉菜单选项?
问题描述:
我在想,是否有可能让jQuery在下拉框中选择<option>
(例如第4项)?
I was wondering if it’s possible to get jQuery to select an <option>
, say the 4th item, in a dropdown box?
<select>
<option></option>
<option></option>
<option></option>
<option></option>
<option></option>
</select>
我希望用户单击一个链接,然后让<select>
框更改其值,就像用户通过单击<option>
选中它一样.
I want the user to click a link, then have the <select>
box change its value, as if the user has selected it by clicking on the <option>
.
答
关于
$('select>option:eq(3)').attr('selected', true);
位于 http://www.jsfiddle.net/gaby/CWvwn/
对于现代版本的jquery,您应该使用 .prop()
而不是.attr()
for modern versions of jquery you should use the .prop()
instead of .attr()
$('select>option:eq(3)').prop('selected', true);