what is the right jquery selector syntax to get all checkboxes with a certain class name?

こ雲淡風輕ζ 提交于 2019-12-11 15:36:15

问题


i have jquery code that loop through all checkboxes.

$('input[type=checkbox]').each(function () {

I now need to make this code more restrictive because i now have other checkboxes on this page. how can i change this so it only loops through all checkboxes with a certain class name?


回答1:


$('input[type=checkbox].class').each(function () {




回答2:


No need to include 'input' in the selector:

$(':checkbox.class')



回答3:


$('input[type=checkbox].myClassName').each(function () {



回答4:


If you're just making a selection, does this really need to be in a loop?

This will return a jQuery object matching your criteria:

$('input.className[type=checkbox]')

if you need it in a loop:

$('input[type=checkbox]').each(function() {
    if ($(this).hasClass('className')) {
         // Do Stuff Here
    }
});



回答5:


how about :

 $('input:checkbox.class')


来源:https://stackoverflow.com/questions/8000592/what-is-the-right-jquery-selector-syntax-to-get-all-checkboxes-with-a-certain-cl

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