Last active
October 26, 2018 19:03
-
-
Save solepixel/a02ee4896e49f6d88f08dc06313cd311 to your computer and use it in GitHub Desktop.
Serialize form inputs
This file contains 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
$.fn.serializeObject = function() { | |
var arrayData = this.serializeArray(), | |
objectData = {}; | |
$.each( arrayData, function() { | |
var value; | |
if ( this.value != null ) { | |
value = this.value; | |
} else { | |
value = ''; | |
} | |
this.name = this.name.replace( '[]', '' ); | |
if ( objectData[ this.name ] != null ) { | |
if ( ! objectData[ this.name ].push ) { | |
objectData[ this.name ] = [ objectData[ this.name ] ]; | |
} | |
objectData[ this.name ].push( value ); | |
} else { | |
objectData[ this.name ] = value; | |
} | |
}); | |
return objectData; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment