Get inputs of different types in a class using jQuery

杀马特。学长 韩版系。学妹 提交于 2019-12-23 18:39:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!