This jquery plugin will map the serialized form data array to JSON Object.
- jquery library.
- import the jquery library.
- import the jquery.serializeObject.js plugin.
$( "form" ).serializeObject();
var rFormat = {
id: 0,
name: "John Doe"
};
$( "form" ).serializeObject( { requiredFormat: rFormat } );
<form>
<input type="text" name="txt01" value="Text 01" />
<input type="text" name="txt02" value="Text 02" />
<input type="submit" value="Submit" />
</form>
<script>
( function( $ ){
$( document ).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
} )( jQuery );
</script>
Object {
txt01: "Text 01",
txt02: "Text 02"
}
<form>
<input type="text" name="txt01[]" value="Text 01" />
<input type="text" name="txt01[]" value="Text 02" />
<input type="submit" value="Submit" />
</form>
<script>
( function( $ ){
$( document).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
} )( jQuery );
</script>
Object {
txt01: Array(2) {
0: "Text 01",
1: "Text 02"
}
}
<form>
<input type="text" name="txt01[name][]" value="Text 01" />
<input type="text" name="txt01[name][]" value="Text 02" />
<input type="text" name="txt01[phone][]" value="000001" />
<input type="text" name="txt01[phone][]" value="000002" />
<input type="submit" value="Submit" />
</form>
<script>
( function( $ ){
$( document ).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
} )( jQuery );
</script>
Object
{
txt01: Object
{
name: Array()
{
0: Text 01
1: Text 02
},
phone: Array()
{
0: 000001
1: 000002
}
}
}
<form>
<input type="text" name="txt01[][name]" value="Text 01" />
<input type="text" name="txt01[][phone]" value="000001" />
<input type="text" name="txt01[][name]" value="Text 02" />
<input type="text" name="txt01[][phone]" value="000002" />
<input type="submit" value="Submit" />
</form>
<script>
( function( $){
$( document ).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
} )( jQuery );
</script>
Object
{
txt01: Array(2)
{
0: Object
{
name: Text 01
phone: 000001
},
1: Object
{
name: Text 02
phone: 000002
}
}
}
<form>
<input type="text" name="txt01[][name]" value="Text 01" />
<input type="text" name="txt01[][phone]" value="000001" />
<input type="text" name="txt01[][name]" value="Text 02" />
<input type="text" name="txt01[][phone]" value="000002" />
<input type="submit" value="Submit" />
</form>
<script>
(function( $ ){
$( document ).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var reqFormat = {
id: 0,
company: "ACN"
};
var jsonObject = $( this ).serializeObject( { requiredFormat: reqFormat } );
console.log(jsonObject);
} );
} );
})( jQuery );
</script>
Object
{
id: 0,
company: "ACN",
txt01: Array(2)
{
0: Object
{
name: Text 01
phone: 000001
},
1: Object
{
name: Text 02
phone: 000002
}
}
}