Skip to content

Instantly share code, notes, and snippets.

@deyvin
Created March 2, 2012 14:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deyvin/1958597 to your computer and use it in GitHub Desktop.
Save deyvin/1958597 to your computer and use it in GitHub Desktop.
Forçar serialize() do jQuery reconhecer checkboxes mesmo não marcado
$(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 ? 'T' : 'F') :
val
};
}).get();
};
});
// créditos: http://tdanemar.wordpress.com/2010/08/24/jquery-serialize-method-and-checkboxes/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment