jQuery inverted selector given a list of values
I need to be able to remove elements based on a list. This has to be easy to do but I'm not seeing the forest through the trees here.
I want to pass in multiple values in a single list. Remove elements not in the list.
<select id="productList">
<option value="CD-6">6 M开发者_StackOverflow中文版onth CD 10K</option>
<option value="CD-12">12 Month CD 10K</option>
<option value="CD-24" selected="selected">24 Month CD 10K</option>
</select>
That is a static list. I want to leave lets say "CD-12, CD-24" and remove CD-6.
$("#productList option:not:contains('CD-12, CD-24')").remove();
Not working, anyone got an idea?
Using contains() is not a good idea, you should be testing the value attribute.
$("#productList option:not([value='CD-12']):not([value='CD-24'])").remove()
$("#productList option:not([value='CD-12'],[value='CD-24'])");
This works properly and gives the desired output. Only CD-12 and CD-24 remain in the listbox.
精彩评论