我如何在使用jQuery的HTML选择中突出显示某些选项 [英] How can I highlight certain options in a HTML select using jQuery
问题描述
我有一个jQuery对HTML选择列表的引用。我需要迭代选项,并检查选项的文本是否匹配正则表达式,如果它是我想添加一个cssclass到选项。如何这样做?
I have a jQuery reference to a HTML select list. I need to iterate the options and check to see if the text of the option matches a regex, if it does I want to add a cssclass to the option. How do I do this?
var ddl = $($get('<%= someddl.ClientID %>'));
我可以利用.each()函数吗?
Can I take advantage of the .each() function somehow?
示例
<select id="someddl">
<option value="21">[21] SomeData ***DATA***</option>
<option value="22">[22] SomeData ***DATA***</option>
<option value="23">[23] SomeData </option>
<option value="24">[24] SomeData </option>
<option value="25">[25] SomeData ***DATA***</option>
</select>
我想为此示例中的项目1,2,5添加一个CSS类。
在c#中我的正则表达式是 Regex expression = new Regex(@\ * \ * \ *);
但是我想通过javascript现在,所以很可能正则表达式看起来有点不同。
I want to add a CSS class to items 1, 2, 5 in this sample.
In c# my regex was Regex expression = new Regex(@"\*\*\*");
but I want to do this through javascript now, so it's likely the regex will look a bit different.
任何想法我可以做些什么来完成这个要求吗?
Any ideas what I can do here to accomplish the requirement?
感谢,
〜ck
Thanks, ~ck
推荐答案
如果你想匹配3个asteriks不需要Regex。尝试以下操作:
As I wrote in my answer to your C# question, you do not need Regex if you want to match 3 asteriks. Try following:
$(document).ready(
function()
{
$('#someddl option:contains("***")').addClass('selected');
}
);
这篇关于我如何在使用jQuery的HTML选择中突出显示某些选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!