In this post Asp.Net jQuery checkbox get
checked items list, we shall see on how to loop through all
the checkboxes in the page and get the list of values checked.
We can loop through all the checkboxes in the page by
filtering the type checkbox in the page elements as follows and get the list of
checked values.
var
selectedIDs = '';
var selectedValues = '';
$("input[type=checkbox][checked]").each(function() {
if (selectedIDs.length == 0) {
selectedIDs
= $(this).attr('id');
selectedValues = $('label[for=' + this.id + ']').html();
}
else {
selectedIDs
+= ", " + $(this).attr('id');
selectedValues += ", " + $('label[for=' + this.id
+ ']').html();
}
});
alert('Selected IDs: ' + selectedIDs);
alert('Selected
Values: ' + selectedValues);
To loop through all the checkboxes in the page on the
click event of a button use the following script.
// Loop through - Get List of Selected Checkboxes in the Page
$('#cmdGetSelectedList').click(function(event) {
event.preventDefault();
var
selectedIDs = '';
var
selectedValues = '';
$("input[type=checkbox][checked]").each(function() {
if
(selectedIDs.length == 0) {
selectedIDs = $(this).attr('id');
selectedValues = $('label[for=' + this.id
+ ']').html();
}
else {
selectedIDs += ", " + $(this).attr('id');
selectedValues += ", " + $('label[for='
+ this.id + ']').html();
}
});
alert('Selected
IDs: ' + selectedIDs);
alert('Selected
Values: ' + selectedValues);
});
To see a
full example using jQuery and Checkbox refer to the post Asp.Net jQuery checkbox Tutorial.
Related Posts
No comments:
Post a Comment