选择选项值中具有部分匹配的选项

我的下拉就像

 Employee Name Employee Name Employee Name Employee Name Employee Name  

我想在值中选择“:4”选项。 我试着用字符串匹配

 $("#taskType option[value^='"+str.match(/(:4)/g)+"']").prop("selected", true); 

但它没有给出结果。 我也尝试过包含函数。

 $("#taskType option[value:contains(':4')]").prop("selected", true); 

仍然没有得到结果。 任何人都可以帮我找到我的代码中的错误。

您可以使用带选择器的属性结尾

 $("#taskType option[value$=':4']").prop("selected", true); 

演示: 小提琴

尝试在此上下文中使用.filter()

 $("#taskType option").filter(function(){ return $(this).val().indexOf(':4') > -1 }).prop("selected", true); 

DEMO

或使用attribute contains(*=)选择器

 $("#taskType option[value*=':4']").prop("selected", true); 

DEMO

你可以这样做:

 $("#taskType option").each(function(){ if($(this).val().indexOf(":4") != -1) { $(this).prop("selected", true) } }); 

FIDDLE DEMO

试试这个

 $("#taskType option[value~=':4']").prop("selected", true);