jquery serialization Form

Source: Internet
Author: User

I. jquery form serialization 1, serialize ()

Description: Serializes the contents of the form to a string that is used for AJAX requests.

Format: var data = $ (form). Serialize ();
Function: Serializes the contents of the form into a single string.

2.serializeArray ()

Description: Serializes a FORM element (similar to the '. Serialize () ' method) to return the JSON data structure.

Note that this method returns a JSON object rather than a JSON string. You need to use a plug-in or a third-party library for string manipulation.

Format: var jsondata = $ (form). Serializearray ();

Function: serializes a page form into a JSON-structured object. Note Not a JSON string

For example, [{"Name": "Lihui", "Age": "20"},{...}] get data for Jsondata[0].name

Both of these methods can set the data parameter to $ (form) When submitting form data using AJAX. Serialize () or $ (form). Serializearray ().

3.Demo

$(function () {            $("#ajaxBtn"). Click (function () {                varPARAMS1 = $ ("#myform"). Serialize ();//name=zhangsan&sex=1&age=20                varPARAMS2 = $ ("#myform"). Serializearray ();//[Object,object, Object]$.ajax ({type:post, url:RegisterAction.action, DATA:PARAMS1, Success:function(msg) {alert (msg);            }                 }); })        })
DemoSecond, form submission package
/*Form parameter serialization Get string form (extensible to JSON form) @id form ID @extParam: Custom Parameter object such as: {pagesize:24,pageindex:1} NOTE: Call this function, 1, need the serial number parameter must be in Form Form 2, the need to parameterize the control must have the property name, generally equal to the ID, such as: <input type= ' text ' name= ' a ' id= ' a '/>*/functionfnparamsstring (ID, extparam) {Debugger; varJSON = $ ("#" +ID). Serializetojson (); if(typeof(extparam)! = "undefined") $.extend (JSON, Extparam); varTempparam = "";  for(varIteminchJSON) {Tempparam+ = "{0}={1}&". Replace (/\{0\}/, item). replace (/\{1\}/, Json[item]); }    returnTempparam;}/*serialize to JSON object*/functionFnparamsjson (ID, extparam) {varJSON = $ ("#" + ID). Serializetojson ();//calling the extension method of the Jquey method    if($.type (extparam)! = "undefined") $.extend (JSON, Extparam); returnJSON;}//serializing a form in JSON format(function($) {$.fn.serializetojson=function () {        varSerializeobj = {};//to define an empty object        varArray = This. Serializearray ();//serialized as JSON object$ (array). each (function () {            if(serializeobj[ This. Name]) {//If there is a name                if($.isarray (serializeobj[ This. Name])) {//if it is an arrayserializeobj[ This. Name].push ( This. Value.trim ()); }                 Else{//not an arrayserializeobj[ This. name] = [serializeobj[ This. Name], This. Value.trim ()]; }            } Else{serializeobj[ This. Name] = This. Value.trim ();        }        }); returnSerializeobj; };})      (JQuery); //method of extending the JQuery object itself (function ($) {}) (jquery); 
Encapsulation Method (form submission)
$ ("#btnSave"). Click (function () {            if(Result.form ()) {varF_site = 0; varF_status = $ ("#f_status"). Prop ("Checked")? 1:0; $("Input[name= ' f_site ']:gt (0)"). each (function () {                    if($( This). Prop ("Checked") ) {F_site= F_site + parseint ($ ( This). Val ());                }                }); Common. Ajax ({URL:"@Url. Action (" savelinemanagemodify ")", Data:fnparamsjson ("Formquery", {"F_site": F_site, "F_status": F_status}) }, function(relt) {if(Relt. Success) {parent.layer.msg (jsoncode["FONT_JRCG"]);                        Parent.callurl ();                    Parent.layer.close (Parent.layer.getFrameIndex (window.name)); } Else{layer.msg (relt).                    Message);            }                }); }        });
called

jquery serialization Form

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.