简体   繁体   中英

Finding all matching options in a select by attribute value

I have a HTML select control with several options. Each option has an attribute named 'linkid'. Example,

"<option linkid = '100' title = 'some title 1' value='1'>some value 1</option>"
"<option linkid = '100' title = 'some title 2' value='2'>some value 2</option>"

Now, I need to find all matching options by attribute 'linkid'.

I tried the following code and it did not work...

var matchingOptions = myselectcontrol.find('[linkid==100]');

var matchingOptions = myselectcontrol.find(':100');

The above code results in syntax error. What am I doing wrong? Is there any way to find matching options by attribute value?

Thanks, Vim

You were so very close, try:

var matchingOptions = myselectcontrol.find('[linkid=100]');

Though it's worth adding that custom attributes, under HTML 4.x, are invalid; under HTML 5 there's the (valid) data-* attributes to hold custom data, which would still be invalid (albeit still functional) under HTML 4.x, but would validate under HTML 5.

Should you consider that a change worth making, your code would become:

<option data-linkid = '100' title = 'some title 1' value='1'>some value 1</option>

And

var matchingOptions = myselectcontrol.find('[data-linkid=100]');

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM