Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

jquery - select an option with partial matching in option value

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use attribute ends with selector

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

Demo: Fiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...