如何使用jQuery选择下拉菜单选项? [英] How to use jQuery to select a dropdown option?

查看:76
本文介绍了如何使用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);

示例,位于 http://jsfiddle.net/gaby/CWvwn/1763/

这篇关于如何使用jQuery选择下拉菜单选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆