Since checkboxes are boolean, you are technically already getting the data you want without doing anything. Since you know what checkboxes are included in the form to begin with, why not let the form submit just the checked ones and then compare it with the list of available checkboxes to know which are off?
Edit
I think I understand where the confusion is.
There are two types of Boolean fields that are very common - one that is either True or False, and one that can be True, False or Null. This is an important difference.
For the first type, using checkboxes is perfect, since by collecting all the 'true' (or 'on') values, you are also automatically collecting the False ones. However, using the second option, doing it with checkboxes could be kinda messy and you should probably use a <select>
instead of a checkbox.
Anyway, if you set your mind to using checkboxes anyway, it is really easy to see if a checkbox is check or not using the .checked
. Here's a jsfiddle the logs each checkbox id and tells the console if it is checked or not. I used console.log() which requires chrome, but you can obviously replace that with whatever you want (alert or a function or what not).
$('button').on('click', function() {
var chbx = $('input[type=checkbox]');
$.each(chbx, function( index, value ) {
console.log(value.id+':'+value.checked);
});
});
If you want to add values to the checkboxes you can add custom attributes to the input tag <input type='checkbox' myattr='whateva' >
and easily access them using jQuery (though that won't work on IE8 or older).