views:

252

answers:

3

I'm using jQuery.serialize to retrieve all data fields in a form.

My problem is that it does not retriev checkboxes that is not checked.

It includes this:

<input type="checkbox" id="event_allDay" name="event_allDay" class="checkbox" checked="checked" />

but not this

<input type="checkbox" id="event_allDay" name="event_allDay" class="checkbox" />

How can I get "values" of checkboxes that is not checked?

+2  A: 

You don't, as serialize is meant to be used as an query string, and in that context, unchecked means it's not in the querystring at all.

If you really want to get the values of unchecked checkboxes, use: (untested off course)

var arr_unchecked_values = $('input[type=checkbox]:not(:checked)').map(function(){return this.value}).get();
azatoth
+3  A: 

jQuery serialize closely mimics how a standard form would be serialized by the browser before being appended to the query string or POST body in the request. Unchecked checkboxes aren't included by the browser, which makes sense really because they have a boolean state -- they're either selected by the user (included) or not selected by the user (not included).

If you need it to be in the serialized, I would ask yourself "why? why not just check for its existence in the data?"

Andy E
It would make it a lot easier to store information in DB. Because then the number of fields from Serialize would equal the number of fields in table. Now I have to contrll which ones are missing.
Steven
+1  A: 

if you want to add unserialized checkboxes to your query string, add the following to your jquery submit function:

moreinfo = '';

$('input[type=checkbox]').each( 
  function(){


    if ($(this).is(':checked'))
        {

        }
        else
        {
        name = $(this).attr('name');
                moreinfo += '&'+name+'=0';

        }
Ashley Ward