English 中文(简体)
Is there javascript (prefer jquery) library that automatically maps form elements to JSON?
原标题:

Background: This is something I have been looking for since before even JSON was known as JSON.

Suppose you have the following javascript variable inside your code:

jsonroot = 
{
     fname : valued 
    , lname : customer 
    ,faves:[ berry , chocolate , mint ]
    ,actors:[
        { fname : brad , lname : pitt }
        ,{ fname : mike , lname : hammer }
    ]
};

You can easily see how this maps to JSON, it is a 1:1 correspondence.

Question: Is there a library that can take this 1:1 correspondence and carry it over to HTML Form elements? For example, I would like to do something like this inside a FORM element:

<input type="text" mapping="jsonroot[ fname ]" id="fname"></input>
<input type="text" mapping="jsonroot[ lname ]" id="lname"></input>

<legend>
    <fieldset>Favorite Flavors</fieldset>
    <input type="text" mapping="jsonroot[ faves ][0]" id="fave_0"></input>
    <input type="text" mapping="jsonroot[ faves ][1]" id="fave_1"></input>
    <input type="text" mapping="jsonroot[ faves ][2]" id="fave_2"></input>
</legend>

<legend>
    <fieldset>Favorite Actors</fieldset>
    <input type="text" mapping="jsonroot[ actors ][0][ fname ]" id="fave_0_fname"></input>
    <input type="text" mapping="jsonroot[ actors ][0][ lname ]" id="fave_0_lname"></input>

    <input type="text" mapping="jsonroot[ actors ][1][ fname ]" id="fave_1_fname"></input>
    <input type="text" mapping="jsonroot[ actors ][1][ lname ]" id="fave_1_lname"></input>
</legend>

Rationale: Sometimes, instead of submitting form variables via POST or GET, I want to pass them into a javascript variable, then do something with the variable, and then send it somewhere.

Writing the HTML Form <-> JSON translation is tedious. I can t be the only person out there who has wanted to to this, so is there a library that handles this automatically?

最佳回答

Use something like Mark Gibson s JSON JQuery plugin. It adds $.toJSON() and $.parseJSON() methods. Use this in combination with code that serializes a form to an object such as Serialize Form to JSON:

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           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;
};
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签