我需要检查<select>是否有一个文本等于特定值的选项。
例如,如果有一个<option value="123">abc</option>,我将寻找"abc"。
有选择器来做这个吗?
我在寻找类似于$('#选择选项[值="123"]');除了文本。
我需要检查<select>是否有一个文本等于特定值的选项。
例如,如果有一个<option value="123">abc</option>,我将寻找"abc"。
有选择器来做这个吗?
我在寻找类似于$('#选择选项[值="123"]');除了文本。
当前回答
我遇到了同样的问题,下面是工作代码:
$("#test option").filter(function() {
return $(this).text() =='Ford';
}).prop("selected", true);
演示:http://jsfiddle.net/YRBrp/83/
其他回答
使用prop而不是attr
$('#mySelect option:contains(' + value + ')').each(function(){
if ($(this).text() == value) {
$(this).prop('selected', 'selected');
return false;
}
return true;
});
这是我在jquery V3.6.0的工作在2022年
$("#select >option").filter( function()
{
if ($(this).text() === "123")
{
$(this).prop("selected", true);
}
});
这对我很有用
$('#mySelect option:contains(' + value + ')').attr('selected', 'selected');
我遇到了同样的问题,下面是工作代码:
$("#test option").filter(function() {
return $(this).text() =='Ford';
}).prop("selected", true);
演示:http://jsfiddle.net/YRBrp/83/
这对我很有用
var options = $(dropdown).find('option');
var targetOption = $(options).filter(
function () { return $(this).html() == value; });
console.log($(targetOption).val());
谢谢你的帖子。