Constructs Ajax parameters, TABLE element json transforms each other

Source: Internet
Author: User

Ajax submits server data and collates the conversion method.

Html:

<form id="FM"Name="FM"action=""> <input name="UserName"Type="text"Value="UserName1"/> </form> <input name="UserId"Id="UserId"Type="text"Value="UserId1"/>

1. Form element Turn QueryString

var q = $ ('#fm, #UserId'//q = username=username1&userid=userid1 

2. Strings, JSON converts each other

var obj = Jquery.parsejson ('{' name ': ' John '}'"  John" );

You can use the Jquery-json plug-in to implement conversions, directly referencing examples

 var  thing = {plugin:  " jquery-json  , Version: 2.3  Span style= "color: #000000;" >}; var  encoded = $.tojson (thing);  //  "{" plugin ":" Jquery-json "," Version ": 2.3} '  var  name = $.evaljson (encoded). Plugin;  //  "Jquery-json"  var  version = $.evaljson (encoded). version;  //  2.3  

3. Form, element to Name,value array

var arr = $ ("#fm, #UserId"). Serializearray (); /* [      {name: ' UserName ', Value: ' "UserName" 1 '},      {name: ' userid ', Value: ' UserID '}  * /

4. Form element to JSON

$.fn.serializeobject =function () {varo = {}; varA = This. Serializearray (); $.each (A, function () {if(o[ This. Name]!==undefined) {            if(!o[ This. Name].push) {o[ This. name] = [o[ This. Name]]; } o[ This. Name].push ( This. value | |"'); } Else{o[ This. Name] = This. value | |"';    }    }); returno;};varobj = $ ('form'). SerializeObject ();/*Obj:objectuserid: "UserId1" UserName: "UserName1" __proto__: Object*/

5. Json2form

$.getjson ('url_to_file', function (data) {    for (var  in data) {        $ ('input[name=''+i+'"]'  ). Val (Data[i]);}    }

Google has found a more powerful plugin in the process http://code.google.com/p/jquery-load-json/

data = {                    "Name":"Emkay Entertainments",                    "Address":"Nobel House, Regent Centre",                    " Contact":"Phone"        }   $('Div#data'). Loadjson (data); <div id="Data"> 

"Name">emkay entertainments for="Address">Address:</label> <span id="Address">nobel House, Regent centre</span> <label for=" Contact">contact by:</label> <span id=" Contact">Phone</span> </div>

Live demo:http://jquery-load-json.googlecode.com/svn/trunk/edit.html?id=

The Ajax commit parameter is to be aware of the type of the commit parameter. such as the Easyui grid parameter can only be JSON, does not support querystring

Constructs Ajax parameters, TABLE element json transforms each other

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.