Sequence a form element as an object code instance

Source: Internet
Author: User

To sequence a form element as an object code instance:
Sometimes it may be more convenient to serialize a form element and then manipulate it, and here's a piece of code that can do this.
The code is as follows:

functionSerializeObject (form) {varo={}; $.each (Form.serializearray (),function(index) {if(o[ This[' Name ']]) {o[ This[' name ']] =o[ This[' name ']] +","+ This[' Value ']; }    Else{o[ This[' name ']] = This[' Value '];   }   }); returno;}

The above code implements our requirements to serialize the form elements into an object, following a simple comment on the code.
A. Code Comment:
1.function SerializeObject (form) {}, the parameter is a form form object, and of course it must be a jquery object.
2.var o={}, declares that an empty object is used to store the name and value values of the form element, the object's property name is the table cell Name property value, and the property value is the value of the form element.
3.$.each (Form.serializearray (), function (index) {}), Form.serializearray () returns an array, the element is the direct amount of the object, and each object has a direct amount of two two name-value pairs, One is the Name/name property value and the other is the Value/value property value. $.each () can traverse the created array.
4.if (o[this[' name ']) {o[this[' name ']]=o[this[' name ']]+ ', ' +this[' value '], and if the object already has a property name for the response, then concatenate the string and reset the property value again. This applies to similar cases where multiple check boxes with the same name are selected.
5.else{o[this[' name ']]=this[' value ';}, create the property and assign a value.
6.return o, returns the generated object.
Two. Related reading:
The 1.serializeArray () function can be found in the Serializearray () method section of jquery .
2. Direct volume of the object can refer to the direct volume of JS object A brief introduction of the chapter.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=13566

For more information, refer to: http://www.softwhy.com/jquery/

Sequence a form element as an object code instance

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.