问题
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