使用jquery选择一个下拉列表选项 [英] use jquery to select a dropdown option
本文介绍了使用jquery选择一个下拉列表选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以在下拉框中选择一个选项,例如第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>
我希望用户点击一个链接,然后选择框更改它的价值,就好像
I want the user to click a link, then have the select box change it's value, as if the user has selected it by clicking on the option.
推荐答案
How about
$('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);
示例 http://jsfiddle.net/gaby/CWvwn/1763/
这篇关于使用jquery选择一个下拉列表选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文