how can I override jquery's .serialize to include unchecked checkboxes
checkbox, jquery, overriding, serialization
Solution
It's a minor change:
.map(function( i, elem ){
var val = jQuery( this ).val();
if ((jQuery(this).checked != undefined) && (!jQuery(this).checked)) {
val = "false";
}
I haven't tested but it seems the most logical approach. Good luck
Problem
I have read quite a few different methods of having html checkboxes get posted to the server, but I am really looking to do it without modifying anything except for $.serialize. I ideally, I would like checked boxes to be posted as on, and unchecked to be posted as 0, empty, or null. I'm a little confused by jquery's inner-workings, but I've got this so far, but it sets unchecked checkboxes to 'on'... Can anyone tell me how to continue this modification below? ``` $.fn.extend({ serializeArray: function() { return this.map(function(){ return this.elements ? jQuery.makeArray( this.elements ) : this; }) .filter(function(){ return this.name && !this.disabled && ( this.checked || !this.checked || rselectTextarea.test( this.nodeName ) || rinput.test( this.type ) ); }) .map(function( i, elem ){ var val = jQuery( this ).val(); return val == null ? null : jQuery.isArray( val ) ? jQuery.map( val, function( val, i ){ return { name: elem.name, value: val.replace( /\r?\n/g, "\r\n" ) }; }) : { name: elem.name, value: val.replace( /\r?\n/g, "\r\n" ) }; }).get(); } }); ```