jQuery:包含选择器替代? [英] jQuery :contains selector alternative?
本文介绍了jQuery:包含选择器替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在其下拉菜单中选择一个选项。当我运行代码时:
I would like to select an option in a drop down menu by its' text. When I run the code,:
var theText =Large;
$(#size option:contains(+ theText +))。attr('selected','selected');
它选择XLarge而不是Large。有没有办法让它选择大? JSFiddle: http://jsfiddle.net/r8wuP/2/
it selects XLarge instead of Large. Is there any way to make it select Large? JSFiddle: http://jsfiddle.net/r8wuP/2/
推荐答案
您可以使用 .filter()以找到完全匹配,:包含选择器也返回部分匹配
You can use .filter() to find an exact match, :contains-selector returns partial matches also
var theText = "Large";
$("#size option").filter(function () {
return $.trim($(this).text()) == theText;
}).attr('selected', 'selected');
演示:小提琴
这篇关于jQuery:包含选择器替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文