Getting selected values of an checkbox array with JQuery
July 1st, 2009
With JQuery it is extremely easy to collect the checked values of an checkbox array. You can use the code below to collect checked items of an array named “itemSelect[]” and give an alert if none of them are checked. This code submits the collected data via ajax to “/ajax_do_something.php” to process the data. If sucessfully processed it refreshes the page to display new data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | var selectedItems = new Array(); $("input[@name='itemSelect[]']:checked").each(function() {selectedItems.push($(this).val());}); if (selectedItems .length == 0) alert("Please select item(s) to delete."); else $.ajax({ type: "POST", url: "/ajax_do_something.php", data: "items=" + selectedItems.join('|'), dataType: "text", success: function (request) { document.location.reload(); }, error: function(request,error){ alert('Error deleting item(s), try again later.'); } } ) |
















Thanks. I send my array by post. The code is:
var selectedItems = new Array();
$(”input[@name='grupo[]‘]:checked”).each(function() {selectedItems.push($(this).val());});
$.post( “/c_reportes/consulta_orgXgrupo”, {’data[]‘:selectedItems}, function(data){
$(’#output’).html(data);
});
Thank’s for the post – just saved me about an hour of working this out!
it is not working in ie8
thanks i spent hours trying to figure out why jquery wouldn’t do all of my checkboxes I wish I would have found this first. Here is what i came up with
function test(){
var boxIds = new Array();
$(”input:checkbox[id=boxId]:checked”).each(function() {clientIds.push($(this).val());});
if (boxIds .length == 0)
alert(”Please select item(s) to delete.”);
else
alert(”boxIds: “+boxIds.join(’|'));
}