Add an entity name to an AJAX form before submitting data

Source: Internet
Author: User

Sometimes we do a reference type in the background such as:The following entities take C # as an example
 Public classorder{ Public stringorderid{Get;Set;}  PublicOrderItem orderitem{Get;Set;}} Public classorderitem{ Public stringproductname{Get;Set;}
Public decimalprice{Get;Set;} }
HTML structure of the foreground page:

<formID= "Form1">    <inputtype= "text"name= "ProductName"value= "Milk" />    <inputtype= "text"name= "Price"value= "1.43" />        <inputtype= "Submit"name= "Submit"value= "Submit" /></form>

background When we get orderitem, we expect the data format to be submitted
{orderitem.productname: "", Orderitem.price: ""}
First, we'll recycle the data from the form. Here's a jquery plugin.

;(function($) {$.fn.serializejson=function () {        varSerializeobj = {}        varArray = This. Serializearray ()varstr = This. Serialize () $ (array). each (function () {            if(serializeobj[ This. Name]) {                if($.isarray (serializeobj[ This. Name])) {serializeobj[ This. Name].push ( This. Value)} Else{serializeobj[ This. name] = [serializeobj[ This. Name], This. Value]} } Else{serializeobj[ This. Name] = This. Value}}) returnSerializeobj}}) (jQuery);
View Code

var formparam=$ ("#form1"). Serializejson (); Console.log (Json.stringify (formparam))

console Output {"OrderItem": {"productName": "Milk", "Price": "1.45"}}then we think that orderitem.productname must be the object's sub-object to use . OperatorFor example {"UserInfo": {"Clsname": "One", "name": "Li Yang"}} The Name property of the access UserInfo can be used Userinfo.nameOkay, got a solution.

var formparam=$ ("#form1"). Serializejson (); Console.log (Json.stringify (Formparam)) Console.log ($. Param (formparam))

Console output:{"OrderItem": {"productName": "Milk", "price": 1.43}}orderitem%5bproductname%5d=milk&orderitem%5bprice%5d=1.43

If you have a better solution welcome message discussion or join the following QQ group to exchange discussion

Technical Exchange QQ Group: 15129679

Add an entity name to an AJAX form before submitting data

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.