问题
Say I have a jquery function within JavaScript and I have this code so far, which works great:
jQuery('div[class="someClass"] input[type="text"]').each( function() { //some code} . . .
In the above code, how can I target multiple types of inputs such as textArea, drop-down list etc. I am pretty new to jQuery so I am not aware of the format a google search did not bring up anything of much relevance! Please help.
回答1:
If you're happy to select all descendent elements that are of these types:
input
textarea
select
button
you can use the :input selector.
jQuery('div.someClass :input').each(function() {...
Otherwise, you can do this in a big selector using the multiple selector as Niklas suggests. One last option is to shorten this by using two selections. This may be slightly slower, but will be significantly more readable:
jQuery('div.someClass').find('input[type="text"], select, textarea').each(function(){...
Note that I have used the class selector to make your selector a bit neater.
回答2:
Separate them with a comma:
jQuery('div[class="someClass"] input[type="text"], div[class="someClass"] textarea, div[class="someClass"] select').each( function() { //some code}
回答3:
Try this:
jQuery('div[class="someClass"] :input').each(function(){
//Do Something..
})
to avoid button elements try:
jQuery('div[class="someClass"] :input:not(:button)').each(function(){
//Do Something..
})
来源:https://stackoverflow.com/questions/6299376/get-inputs-of-different-types-in-a-class-using-jquery