views:

48

answers:

3

Basically what I need to do is run through a series of checkbox if it is checked I need to submit its value using ajax. Now I can't seem to find a way to run through each checkbox one at a time. Here's the code on my page.

$(document).ready(function(){
$('#submit').click(function(){

});
});

<?php
/**
 * Include vB core.
 */ 
require_once('./global.php');
require_once(DIR . '/includes/functions_bigthree.php');
require_once(DIR . '/includes/functions_forumlist.php');

/**
 * Grab the divisions from the database.
 */
$query = $db->query_read("SELECT division FROM rollcall_divisions");
while($array = $db->fetch_array($query)){
echo "<input type='checkbox' value=".$array[division].">".$array[division]."<br>";
}
?>

<input type="submit" id="submit">
A: 
$('#yourform').find(':checkbox').each( function() {
    // do ajax stuff on $(this)
});
Ken Redler
+1  A: 

You can use the :checked selector for your checkboxes and the submit the ajax request for them using the each() method:

$('input[type=checkbox]:checked').each(function(){
    // submit your ajax request        
});

To actually submit the ajax request, jQuery has the $.ajax() method.

Pat
You used a radio instead of a checkbox but I get the idea.
Arimil
Whoops...you can thank delicious beer for that. Fixed now.
Pat
+3  A: 

You can use :checkbox filter selector and each like this:

$('#submit').click(function(){
  $(':checkbox').each(function(){
     if ($(this)).is(':checked') {
       // your code....
     }
  });
});

The :checkbox targets all checkboxes on the page and then each is used to iterate over each of them. Later in the condition is is used with :checked filter selector to know which of these checkboxes is actually checked.

Sarfraz
+1, more thorough than mine.
Ken Redler