JQuery checkbox loop
Given a form (id = "my_form"
), how do I loop over all checkboxes of the form, checking if the given one is checked or not and gr开发者_运维技巧abbing its name/value?
I know how to check a checkbox status given its value, but I need to loop without knowing in advance which checkboxes exist. Thanks.
Example: http://jsfiddle.net/C6Kau/
$('#my_form :checkbox').each(function(){
if( this.checked ) {
alert( 'checked: ' + this.name + ' ' + this.value );
} else {
alert( 'Not checked: ' + this.name + ' ' + this.value );
}
});
the following code will loop over all of the checked checkboxes.
$(':checkbox', '#my_form').filter(':checked').each(function(){
var name = $(this).val();
});
var box, boxes = $('#myform :checkbox'), i = boxes.length;
while (i--) {
box = boxes[i];
if (box.checked) {
alert ("I'm checked");
} else {
alert ("I'm not checked");
}
}
精彩评论