通过JQuery选择选择中的选项 [英] Selecting options in a select via JQuery

查看:95
本文介绍了通过JQuery选择选择中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以选择以下选项:

<select>
     <option value="1">A</option>
     <option value="2">B</option>
     <option value="3">C</option>
</select>

我不知道值是什么,但是在我的JQuery代码中,我想选择选项二,因为我知道文本是B(但无其他内容).

I don't know what the values are, but in my JQuery code, I want to select option two because I know that the text is B (but nothing else).

怎么办?

推荐答案

您可以使用 :contains() 选择器:

You can use the :contains() selector:

$('option:contains("B")').prop('selected', true);

或者:

$('option')
    .filter(function() {
        return this.text == 'B'; 
    })
    .prop('selected', true);

这篇关于通过JQuery选择选择中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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