Skip to content

Instantly share code, notes, and snippets.

@mafigit
Created December 15, 2015 17:33
Show Gist options
  • Save mafigit/770cd4927a1dbc40b87a to your computer and use it in GitHub Desktop.
Save mafigit/770cd4927a1dbc40b87a to your computer and use it in GitHub Desktop.
serializeAllArray (same as serializeArray but with disabled fields included). can be used to completly restore a empty form
(function ($) {
$.fn.serializeAllArray = function () {
var serialize_array =
$('input',this).toArray().reduce(function(acc, input) {
var form_attr = input.name;
var form_value = $(input).val();
if ($(input).is(":checkbox") && !$(input).is(":checked")) {
form_value = 0;
}
if($(input).is(":radio")) {
form_value = $("input[name='" + form_attr + "']:checked").val();
}
if (form_attr) {
acc.push({
name: form_attr,
value: form_value
});
}
return acc;
},[]);
return serialize_array;
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment