选择一个选项值与部分匹配的选项 [英] select an option with partial matching in option value
本文介绍了选择一个选项值与部分匹配的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的下拉菜单就像
< select id =taskType>
< option value =xyz:1>员工名称< / option>
< option value =abc:2>员工名称< / option>
< option value =123:3>员工姓名< / option>
< option value =efg:4>员工姓名< / option>
< option value =hij:5>员工姓名< / option>
< / select>
我想选择值为:4的选项。
i试着用字符串匹配
$ $ $#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);
演示:小提琴
my dropdown is like
<select id="taskType">
<option value="xyz:1">Employee Name</option>
<option value="abc:2">Employee Name</option>
<option value="123:3">Employee Name</option>
<option value="efg:4">Employee Name</option>
<option value="hij:5">Employee Name</option>
</select>
i want to select the option with ":4" in the value. i tried with string matching
$("#taskType option[value^='"+str.match(/(:4)/g)+"']").prop("selected", true);
but its not giving the result. I tried with contains function too.
$("#taskType option[value:contains(':4')]").prop("selected", true);
still didn't got the result. can anyone help me to find whats the mistake in my code.
解决方案
You can use attribute ends with selector
$("#taskType option[value$=':4']").prop("selected", true);
Demo: Fiddle
这篇关于选择一个选项值与部分匹配的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文