Home > JQuery, Javascript, Programming > Getting selected values of an checkbox array with JQuery

Getting selected values of an checkbox array with JQuery

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

JQuery, Javascript, Programming

  1. Monica Baquerizo
    January 15th, 2010 at 21:00 | #1

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

  2. May 13th, 2010 at 13:22 | #2

    Thank’s for the post – just saved me about an hour of working this out!

  1. No trackbacks yet.