javascript - Jquery serialize multiple form and send as JSON with different key -
hi want serialize multiple forms , send json different key.
i want json
"{userdetails:[{'name':'abc'}],contactdetails:[{'mobile':'123456'}]}"
i tried this
<form id="form1"> name : <input type="text" name="name" /> </form> <form id="form2"> mobile : <input type="text" name="mobile" /> </form> <input type="button" onclick="submitform();" value="save" /> <script> function submitform(){ var jsonstring = $('#form1,#form2').serializearray(); } </script>
serializearray
creates array of objects. if want put object, need explicitly. , need call json.stringify
convert json.
function submitform() { var details = { userdetails: $("#form1").serializearray(), contactdetails: $("#form2").serializearray() }; var jsonstring = json.stringify(details); ... }
Comments
Post a Comment