JQuery Serialize Method and Checkboxes
JQuery’s serialize method is handy when you want to convert the values of a form to a standard URL encoded query string, like so:
$("#myform").serialize(); // 'a=1&b=2&c=3'
However, only successful controls are included in the query string. That means checkboxes may not be serialized the way you expect them to. Checked checkboxes will be included with name only, and unchecked boxes won’t be included at all. So while you may expect a query string like:
'checkbox1=true&checkbox2=false'
What you’ll actually get is:
'checkbox1='
Sometimes you want the true/false type string though. Here’s a plugin that replaces the standard jQuery serialize with one that returns just that:
(function ($) { $.fn.serialize = function (options) { return $.param(this.serializeArray(options)); }; $.fn.serializeArray = function (options) { var o = $.extend({ checkboxesAsBools: false }, options || {}); var rselectTextarea = /select|textarea/i; var rinput = /text|hidden|password|search/i; return this.map(function () { return this.elements ? $.makeArray(this.elements) : this; }) .filter(function () { return this.name && !this.disabled && (this.checked || (o.checkboxesAsBools && this.type === 'checkbox') || rselectTextarea.test(this.nodeName) || rinput.test(this.type)); }) .map(function (i, elem) { var val = $(this).val(); return val == null ? null : $.isArray(val) ? $.map(val, function (val, i) { return { name: elem.name, value: val }; }) : { name: elem.name, value: (o.checkboxesAsBools && this.type === 'checkbox') ? //moar ternaries! (this.checked ? 'true' : 'false') : val }; }).get(); }; })(jQuery);
This plugin is based on the actual jQuery serialize method, so most of it is not my code. Usage:
$("#myform").serialize({ checkboxesAsBools: true });
The default behaviour with no options passed in (or checkboxesAsBools set to false) is just like the standard jQuery serialize method.
//moar ternaries, moar!
Thank you very much for this very informative posting … saved me a bundle in time.
Thanks very much for this, very handy!
I’ve copied it into a “Gist” on GitHub so that anyone can contribute modifications etc – please tell me if you’d rather I remove this and keep the only public version here!
FYI, I modified the “jquery.deserialize” plugin to handle this “checkboxesAsBools” option also: https://github.com/TaoK/jquery.deserialize
(oops, just noticed I never listed the gist address in my original comment! it’s here: https://gist.github.com/1572512 )
Great job my friend. I was looking for this solution. tnx 😉
it’s great!
Thank you!
I cannot say how much I thank you for this…save me a lot of time.
Very nice piece of code! Just what is was looking for! I’m using it in combination with MVC4 web-api.
Thank you. its very help me at this time. 🙂
Again… Thanks.
Thank you.
Thank you 🙂
thanks a lot for this!
You rock. Thanks for this!
Thank you so much Thomas.. I was stuck with this for days..
Watch out if you’re using HTML5 input types like type=”email”. Then change on line 13
var rinput = /text|email|hidden|password|search/i;
Thank you – Thank you – Thank you !!!