我需要检查<select>是否有一个文本等于特定值的选项。
例如,如果有一个<option value="123">abc</option>,我将寻找"abc"。
有选择器来做这个吗?
我在寻找类似于$('#选择选项[值="123"]');除了文本。
我需要检查<select>是否有一个文本等于特定值的选项。
例如,如果有一个<option value="123">abc</option>,我将寻找"abc"。
有选择器来做这个吗?
我在寻找类似于$('#选择选项[值="123"]');除了文本。
当前回答
这对我很有用
var options = $(dropdown).find('option');
var targetOption = $(options).filter(
function () { return $(this).html() == value; });
console.log($(targetOption).val());
谢谢你的帖子。
其他回答
我遇到了同样的问题,下面是工作代码:
$("#test option").filter(function() {
return $(this).text() =='Ford';
}).prop("selected", true);
演示:http://jsfiddle.net/YRBrp/83/
这可能会有所帮助:
$('#test').find('option[text="B"]').val();
演示小提琴
这将为你提供带有文本B的选项,而不是包含文本B的选项。
对于最新版本的jQuery,上述问题无法解决。正如下面Quandary所评论的,这是jQuery 1.9.1的工作原理:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();
更新的小提琴
这在jQuery 1.6中是有效的(注意:在开括号前是冒号),但是在新版本(当时是1.10)中无效。
$('#mySelect option:[text=abc]")
使用以下
$('#select option:contains(ABC)').val();
这是在下拉列表中选择文本的最佳方法。
$("#dropdownid option:contains(your selected text)").attr('selected', true);