Functions used with jquery serializeArray () to facilitate form submission

Source: Internet
Author: User

. SerializeArray () serializes table elements (similar to the '. serialize ()' method) and returns JSON data structure data. (From the jquery document ).
There is a form window with the code: Copy codeThe Code is as follows: <form action = "" method = "post" id = "tf">
<Table width = "100%" cellspacing = "0" cellpadding = "0" border = "0">
<Tr>
<Th> name: </th>
<Td>
<Input type = "text" id = "txtUserName" name = "UserName"/>
</Td>
<Th> mobile phone number: </th>
<Td>
<Input type = "text" name = "Mobile" id = "txtMobile" maxlength = "11"/>
</Td>
</Tr>
<Tr>
<Td style = "text-align: center;" colspan = "2">
<Input type = "button" value = "Submit" style = "padding-top: 3px;" name = "butsubmit" id = "butsubmit"/>
</Td>
</Tr>
</Table>
</Form>

JavaScript code Processing Form:Copy codeThe Code is as follows: <script>
$ (Function (){
$ ("# Butsubmit"). click (function (){
Var data = convertArray ($ ("# tf"). serializeArray ());
$. Post (url, data, function (d) {}, "json ");
});
})
Function convertArray (o) {// mainly recommends this function. It converts the serialized value of jquery into the name: value format.
Var v = {};
For (var I in o ){
If (typeof (v [o [I]. name]) = 'undefined') v [o [I]. name] = o [I]. value;
Else v [o [I]. name] + = "," + o [I]. value;
}
Return v;
}
</Script>

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.