简体   繁体   中英

select all elements with certain name but without some

if I'm selecting all input elements with name ending Phone like

$("input[name$='Phone']")..

how can I limit this selection further in a sense that I want to select all input elements with name ending Phone but without element with name zPhone and kkPhone and ooo2tPhone for example.

You can use not selector:

$("input[name$='Phone']").not('[name$="zPhone"]').not('[name$="kkPhone"]')

But for simplicity I would have names that would end with Phone including an underscore: my_Phone, your_Phone, our_Phone, etc. and then just use:

$("input[name$='_Phone']")

https://api.jquery.com/not-selector/
http://api.jquery.com/not/

 $("input[name$='Phone']:not([name='megaPhone'],[name='cellPhone'])").css({border:"2px solid red"}); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- YES --> <input name="mobilePhone"> <input name="telePhone"> <!-- NOT --> <input name="cellPhone"> <input name="megaPhone"> 


Using http://api.jquery.com/filter/ and regex:

 $("input[name$='Phone']").filter(function(){ return !this.name.match(/^(cell|mega)Phone$/); }).css({border:"2px solid red"}); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- YES --> <input name="mobilePhone"> <input name="telePhone"> <!-- NOT --> <input name="cellPhone"> <input name="megaPhone"> 

/^(cell|mega|dicta)Phone$/g
^ assert position at start of the string
1st Capturing group (cell|mega|dicta)
1st Alternative: cell
cell matches the characters cell literally (case sensitive)
2nd Alternative: mega
mega matches the characters mega literally (case sensitive)
3rd Alternative: dicta
dicta matches the characters dicta literally (case sensitive)
Phone matches the characters Phone literally (case sensitive)
$ assert position at end of the string

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