A function 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,Code:

<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:

<SCRIPT>
$ (Function ()
{
$ ("# Butsubmit"). Click (function (){
VaR DATA =
Convertarray ($ ("# TF"). serializearray ());
$. Post (URL, Data, function (d)
{}, "JSON ");
});
})
Function convertarray (o ){
// This function is recommended. 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>

. 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:

<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:

<SCRIPT>
$ (Function ()
{
$ ("# Butsubmit"). Click (function (){
VaR DATA =
Convertarray ($ ("# TF"). serializearray ());
$. Post (URL, Data, function (d)
{}, "JSON ");
});
})
Function convertarray (o ){
// This function is recommended. 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.