YUI html form serialization html form serialize

Source: Internet
Author: User

Form serialization in yui3. I found that there are not many yui tutorials for the Helper's house. The most popular is not necessarily the best. I personally prefer yui over jquery.

Usage:

1. serializeForm ('# formid'), return a = 55 & B = option1 & B = option2

Ii. serializeForm ('# formid', true), return object {afile: '55', bfield: ['option1', 'option2]}

Copy to ClipboardReference: [www.bkjia.com] var serializeForm = (function (){
Var namevalues = {};
Function add (name, value ){
If (namevalues [name]) {
If (Y. Lang. isArray (namevalues [name]) {
Namevalues [name]. push (value );
} Else {
Namevalues [name] = [namevalues [name];
Namevalues [name]. push (value );
}
} Else {
Namevalues [name] = value;
}
}
Return function (form, obj ){
Namevalues = {};
Var formNode;
If (form ){
If (Y. Lang. isString (form )){
FormNode = Y. one (form );
} Else {
FormNode = form;
}
}
Var items = formNode. all ('input [name] [type = text], input [name] [type = hidden], input [name] [type = radio]: checked, input [name] [type = check]: checked, select [name], textarea [name] ');

Items. each (function (nel ){
If (nel. get ('tagname'). toUpperCase () = "SELECT "){
Nel. get ('options'). each (function (op ){
If (op. get ('selected ')){
Add (nel. get ('name'), op. get ('value '));
}
});
} Else {
Add (nel. get ('name'), nel. get ('value '));
}
});
If (obj ){
Return namevalues;
} Else {
Y. QueryString. stringify (namevalues );
}
};
})();

Related Article

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.