如何使用jQuery在下拉列表中通过文本值选择项目? [英] How do I select an item by its text value in a dropdown using jQuery?

查看:72
本文介绍了如何使用jQuery在下拉列表中通过文本值选择项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下选择,并且不知道用于提前选择一个项目的值,就像在这个问题或我想要选择的项目的索引,如果我知道如C选项那样的文本值,我如何可以使用jQuery选择一个选项?

If I had the following select, and did not know the value to use to select an item in advance like in this question or the index of the item I wanted selected, how could I select one of the options with jQuery if I did know the text value like Option C?

<select id='list'>
<option value='45'>Option A</option>
<option value='23'>Option B</option>
<option value='17'>Option C</option>
</select>


推荐答案

var option;
$('#list option').each(function() {
    if($(this).text() == 'Option C') {
        option = this;
        return false;
    }
});

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

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