如何通过文本选择选项?

时间:2010-09-19 02:15:19

标签: jquery

我需要检查<select>是否有一个文本等于特定值的选项。

例如,如果有<option value="123">abc</option>,我会寻找“abc”。

是否有选择器来执行此操作?

我正在寻找与$('#select option[value="123"]');类似的内容,但文字。

17 个答案:

答案 0 :(得分:297)

这可能有所帮助:

$('#test').find('option[text="B"]').val();

Demo fiddle

这会为您提供包含文字B的选项,而不包含包含B文字的选项。希望这有帮助

对于jQuery的最新版本,上面的内容不起作用。如下面Quandary所述,这适用于jQuery 1.9.1:

$('#test option').filter(function () { return $(this).html() == "B"; }).val();

Updated fiddle

答案 1 :(得分:126)

您可以使用:contains() selector选择包含特定文字的元素 例如:

$('#mySelect option:contains(abc)')

要检查给定<select>元素是否具有此选项,请使用.has() method

if (mySelect.has('option:contains(abc)').length)

要查找包含此类选项的所有<select>,请使用:has() selector

$('select:has(option:contains(abc))')

答案 2 :(得分:30)

以前的建议都没有在jQuery 1.7.2中为我工作,因为我试图根据文本框中的值设置列表的选定索引,并且一些文本值包含在多个选项中。我最终使用了以下内容:

$('#mySelect option:contains(' + value + ')').each(function(){
    if ($(this).text() == value) {
        $(this).attr('selected', 'selected');
        return false;
    }
    return true;
});

答案 3 :(得分:16)

我在下面遇到的问题是工作代码:

1

演示:http://jsfiddle.net/YRBrp/83/

答案 4 :(得分:13)

这对我有用:$("#test").find("option:contains('abc')");

答案 5 :(得分:11)

这是下拉列表中选择文字的最佳方法。

$("#dropdownid option:contains(your selected text)").attr('selected', true);

答案 6 :(得分:4)

我尝试了其中的一些内容,直到我找到了一个在Firefox和IE中工作的东西。这就是我想出来的。

$("#my-Select").val($("#my-Select" + " option").filter(function() { return this.text == myText }).val());

另一种以更易读的方式编写它的方式:

var valofText = $("#my-Select" + " option").filter(function() {
    return this.text == myText
}).val();
$(ElementID).val(valofText);

伪代码:

$("#my-Select").val( getValOfText( myText ) );

答案 7 :(得分:3)

使用以下

$('#select option:contains(ABC)').val();

答案 8 :(得分:1)

这将在jQuery 1.6中工作(在开始括号前注释冒号),但在较新的版本(当时为1.10)上失败。

$('#mySelect option:[text=abc]")

答案 9 :(得分:1)

对于jquery版本1.10.2,下面为我工作

  var selectedText="YourMatchText";
    $('#YourDropdownId option').map(function () {
       if ($(this).text() == selectedText) return this;
      }).attr('selected', 'selected');
    });

答案 10 :(得分:0)

使用 prop 代替 attr

$('#mySelect option:contains(' + value + ')').each(function(){
    if ($(this).text() == value) {
        $(this).prop('selected', 'selected');
        return false;
    }
    return true;
});

答案 11 :(得分:0)

这对我有用:

var result = $('#SubjectID option')
            .filter(function () 
             { return $(this).html() == "English"; }).val();

result变量将返回匹配的文本值的索引。现在,我将使用它的索引进行设置:

$('#SubjectID').val(result);

答案 12 :(得分:0)

这项工作对我来说

$('#mySelect option:contains(' + value + ')').attr('selected', 'selected');

答案 13 :(得分:-1)

As described in this answer, you can easily create your own selector for hasText. This allows you to find the option with $('#test').find('option:hastText("B")').val(); Here's the hasText method I added: if( ! $.expr[':']['hasText'] ) { $.expr[':']['hasText'] = function( node, index, props ) { var retVal = false; // Verify single text child node with matching text if( node.nodeType == 1 && node.childNodes.length == 1 ) { var childNode = node.childNodes[0]; retVal = childNode.nodeType == 3 && childNode.nodeValue === props[3]; } return retVal; }; }

答案 14 :(得分:-1)

这也可以。

$(&#39; #test&#39;)。找到(&#34;选择选项:包含(&#39; B&#39;)&#34;)。过滤器(&#34;:已选择& #34);

答案 15 :(得分:-1)

您可以遍历这些选项,或者将相同的文本放在该选项的另一个属性中,然后选择该选项。

答案 16 :(得分:-2)

这对我有用

var options = $(dropdown).find('option');
var targetOption = $(options).filter(
function () { return $(this).html() == value; });

console.log($(targetOption).val());

感谢所有帖子。

相关问题