Last active
February 20, 2021 19:34
-
-
Save gintsmurans/25bfafc7ca43d902b3ab0485e1d49f99 to your computer and use it in GitHub Desktop.
Serialize form fields recursively into json type object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery addons | |
// Serialize form fields recursively into json type object, | |
// so for example <input type="text" name="record[type][x]" value="1"> becomes {type: {x: 1}} | |
var | |
rbracket = /\[\]$/, | |
rarrayKeys = /\[(.*?)\]/g, | |
rarrayKeysPrefix = /^(.*?)\[.*/g, | |
rCRLF = /\r?\n/g, | |
rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i, | |
rsubmittable = /^(?:input|select|textarea|keygen)/i, | |
rcheckableType = /^(?:checkbox|radio)$/i; | |
jQuery.fn.extend( { | |
serializeObject: function() { | |
var items = this.map( function() { | |
// Can add propHook for "elements" to filter or add form elements | |
var elements = jQuery.prop( this, "elements" ); | |
return elements ? jQuery.makeArray( elements ) : this; | |
} ) | |
.filter( function() { | |
var type = this.type; | |
// Use .is( ":disabled" ) so that fieldset[disabled] works | |
return this.name && !jQuery( this ).is( ":disabled" ) && | |
rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) && | |
( this.checked || !rcheckableType.test( type ) ); | |
} ); | |
var keysToArray = function(key, val, match, result) { | |
if (match == null) { | |
return result; | |
} | |
var | |
this_key = match[1], | |
next_match = rarrayKeys.exec(key), | |
next_key = (next_match != null ? next_match[1] : null); | |
if (next_match == null) { | |
if (this_key== '') { | |
result.push(val); | |
} else { | |
result[this_key] = val; | |
} | |
return result; | |
} | |
// Do this again | |
if (typeof result[this_key] == 'undefined') { | |
result[this_key] = (next_key == '' ? [] : {}); | |
} | |
keysToArray(key, val, next_match, result[this_key]); | |
// Return result | |
return result; | |
}; | |
var elementArrayToObject = function(array) { | |
var result = {}; | |
array.each(function(i, elem){ | |
var val = jQuery( this ).val(); | |
if (val == null) { | |
return; | |
} | |
if (jQuery.isArray(val)) { | |
result[elem.name] = elementArrayToObject(val); | |
} | |
var match = rarrayKeys.exec(elem.name); | |
if (match == null) { | |
result[elem.name] = val.replace(rCRLF, "\r\n"); | |
} else { | |
var prefix = elem.name.replace(rarrayKeysPrefix, '$1'); | |
if (typeof result[prefix] == 'undefined') { | |
result[prefix] = {}; | |
} | |
keysToArray(elem.name, val.replace(rCRLF, "\r\n"), match, result[prefix]); | |
} | |
}); | |
return result; | |
}; | |
return elementArrayToObject(items); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LOVE YOU BRO