The form serialization of any jquery object eliminates the hassle of large-scale spelling of form data when submitting requests, and supports key-value pairs <name&value> formats and JSON formats.

Source: Internet
Author: User
Tags tojson

http://zhengxinlong.iteye.com/blog/848712

The form serialization of any jquery object eliminates the hassle of large-scale spelling of form data when submitting requests, and supports key-value pairs <name&value> formats and JSON formats.

///<reference name= "Jquery.js" description= "1.3.2 versions above"/>/*!* extended jquery form serialization function: {version:1.2, Author:Eric.Zheng, createdate:2010-12-21}** eliminated Jquery.serialize () Only the limitation of the serialization of a form * The plugin can serialize any jquery object * The return data format has two:1.<name&value> (default) 2.json** call methods: $ (DOM). form_serialize (dataType); * Parameters (can be omitted): DataType: Default is HTML, that is, the return data format is <name&value>; to return the JSON format, DataType = json;* return Data: Serializing form data * * Bug fix: Fixed a missing data obtained when multiple DOM elements used the same name attribute in version 1.0. ***/(function ($) {    varFormjson = {}; varCurrentform =NULL; $.fn.form_serialize=function(dataType) {Currentform= $( This); Formjson= {}; varDoms = Currentform.find (' [name] '); $.each (Doms,function(Index, DOM) {varDomname = $ (DOM). attr (' name '); if(!Formjson[domname]) {Formjson[domname]= {Name:domname, type: $ (DOM). attr (' type '), Doms:currentForm.find (' [name= ' + Domname + '] ') };        }        }); returnGetResult (DataType);    }; varGetResult =function(dataType) {varD ={toJson:function () {                vardata = {}; $.each (Formjson,function(key, JSON) {Data[key]=Getval (JSON);                }); returndata; }, ToString:function () {                varval = '; varindex = 0; $.each (Formjson,function(key, JSON) {varprefix = ' & '; if(index = = 0) prefix = "; Index++; Val+ = prefix + key + ' = ' +Getval (JSON);                }); returnVal;        }        }; returnDataType = = ' json '?D.tojson (): d.tostring (); }    varGetval =function(JSON) {varMethods ={getdefaultval:function(DOM) {return$ (DOM). Val (); }, Getselectval:function(DOM) {varval = '; varSelectType = $ (DOM). attr (' type '); if(SelectType = = ' Select-multiple ') {                    varItems =$ (DOM). Val (); if(Items = =NULL)return‘‘;  for(vari = 0; i < items.length; i++) {Val+ = i = = 0? Items[i]: (', ' +Items[i]); }                    returnVal; } Else {                    return$ (DOM). Val (); }}, Getradioval:function(DOM) {return$ (DOM). attr (' checked ')? $ (DOM). Val ():NULL; }, Getcheckboxval:function(DOM) {returnMethods.getradioval (DOM);        }        }; varDispacher =function(Type, DOM) {Switch(type) { Case' Text ':                 Case' Password ':                 Case' Hidden ':                 Case' TextArea ':                    returnMethods.getdefaultval (DOM);  Case' Select-one ':                 Case' Select-multiple ':                    returnMethods.getselectval (DOM);  Case' Radio ':                    returnMethods.getradioval (DOM);  Case' checkbox ':                    returnMethods.getcheckboxval (DOM); default:                    return‘‘;        }        }; varDomtype =JSON.        Type; varDoms =$ (JSON.        Doms); varCount =doms.length; if(Count > 1) {            varval = '; varindex = 0;  for(vari = 0; I < count; i++) {                varv =Dispacher (Domtype, Doms.eq (i)); if(v = = "| | v = =NULL|| v = =undefined)Continue; Val+ = index++ = = 0? Dispacher (Domtype, Doms.eq (i)): (', ' +Dispacher (Domtype, Doms.eq (i))); }            returnVal; } Else {            returnDispacher (Domtype, Doms); }    };}) (jQuery);

The form serialization of any jquery object eliminates the hassle of large-scale spelling of form data when submitting requests, and supports key-value pairs <name&value> formats and JSON formats.

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.