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

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

有选择器来做这个吗?

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


当前回答

这对我有用:$(“#test”).find(“选项:contains('abc')”);

其他回答

这对我很有用

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

这对我有用:$(“#test”).find(“选项:contains('abc')”);

使用prop而不是attr

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

这在jQuery 1.6中是有效的(注意:在开括号前是冒号),但是在新版本(当时是1.10)中无效。

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

这对我来说很管用:

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

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

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