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.'); } } ) |













Burc Sade (aka laforge) is a full time application developer (mainly web applications), part time entrepreneur located in Istanbul, Turkey. This site is his blog about the projects going on, little programming tricks, Internet, travel, photography and other bits and pieces.
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!