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

例如,如果有一个<option value="123">abc</option>,我将寻找"abc"。

有选择器来做这个吗?

我在寻找类似于$('#选择选项[值="123"]');除了文本。


当前回答

这也是可行的。

$(" #测试”)。找到(“选择选项:包含(B)”).filter(":选择");

其他回答

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

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

下面的jquery 1.10.2版本适合我

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

您可以遍历选项,也可以将相同的文本放在选项的另一个属性中并使用该属性进行选择。

这对我来说很管用:

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

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

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

之前的建议在jQuery 1.7.2中都不适用,因为我试图根据文本框中的值设置列表的选定索引,而一些文本值包含在多个选项中。我最终使用了以下方法:

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