The form Element JSON is converted to each other by constructing AJAX parameters. ajaxjson

Source: Internet
Author: User

The form Element JSON is converted to each other by constructing AJAX parameters. ajaxjson

Ajax submits server data and sorts out 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. Convert form elements to QueryString

var q = $('#fm,#UserId').serialize(); //q = UserName=UserName1&UserId=UserId1 

2. String, JSON Conversion

var obj = jQuery.parseJSON('{"name":"John"}');alert( obj.name === "John" ); 

You can use the jquery-json plug-in to implement conversion and directly reference the example.

var thing = {plugin: 'jquery-json', version: 2.3};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. Convert form elements to JSON

$.fn.serializeObject = function(){var o = {};var a = 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 || '';}});return o;};var obj = $('form').serializeObject();/*obj: ObjectUserId: "UserId1"UserName: "UserName1"__proto__: Object*/ 

5. JSON2FORM

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

Google found a more powerful plug-in 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">

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.