JAVASCRIPT-JS serialization of form forms [json strings, arrays, objects]

Source: Internet
Author: User

1. Serializing to a string

$ ("#Form"). Serialize (); //name=zhangsan&sex=1&age=20

2. Serializing arrays

$ ("#form"). Serializearray (); //[object, object, object]

3. Serializing to an object

    functionGetformjson (form) {varo = {}; varA =$ (form). Serializearray (); $.each (a,function () {            if(o[ this. name]!==Undefined) {                if(!o[ this. Name].push) {o[ this. name] = [o[ this. name]]; } o[ this. Name].push ( this. value | | ‘‘); } Else{o[ this. name] = this. value | | ‘‘;        }        }); returno; }

4.ajax passing a normal array

 var  deletenum= []; //  Defines the array to be passed  deletenum.push ("1" );d Eletenum.push (  "2" );d eletenum.push ( "3"); //  $.ajax ({type:  "post" " Deletenum.do " true , //  must be specified as true  success:< Span style= "color: #0000ff;" >function   if   (data.success) {deletenum  = []; }    }});

Back-end Code

 public ajaxresult deletenum (string[] deletenum) {    new  ajaxresult ();     // this time, we've got the deletenum array value.    return ajaxresult;}

5.form form submission Custom Object array

<formID= "form"name= "form"Method= "post">
<input type= "hidden" name= "table" value= "user" >
<Table> <TR> <TD><inputtype= "text"name= "userlist[0].name"/></TD> <TD><inputtype= "text"name= "userlist[0].password"/></TD> </TR> <TR> <TD><inputtype= "text"name= "userlist[1].name"/></TD> <TD><inputtype= "text"name= "userlist[1].password"/></TD> </TR> <TR> <TD><inputtype= "text"name= "userlist[2].name"/></TD> <TD><inputtype= "text"name= "userlist[2].password"/></TD> </TR> </Table></form>

Ajax Submissions

$ ("#form"). Serializearray ()

Back-end Receive

     public classFormlist {PrivateString table; PrivateArraylist<user>userlist;  publicString getTable () {returntable; }         public voidsettable (String Table) { this. Table =table; }         publicArraylist<user>getuserlist () {returnuserlist; }         public voidSetuserlist (arraylist<user>Userlist) {             this. userlist=userlist; }}
 public ajaxresult saveupdateuser (formlist list) {    list<User> userlist = list.getuserlist ();}










JAVASCRIPT-JS serialization of form forms [json strings, arrays, objects]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.