Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery how to load some json records into a form fields?

i have a json file:

{
   "data":"Click",
   "size":"Here"
}

and a form:

<form>
   First name: <input type="text" name="firstname" /><br />
   Last name: <input type="text" name="lastname" />
</form>

i was wondering what is the correct sintax for loading multiple json records into multiple form elements?? Something like a form that u want to u want to modify and the values get queered into those fields

i know i can use:

$getJSON('link_to_json_file' function() {

});

but i'm stuck here thanks

like image 487
Patrioticcow Avatar asked Apr 09 '11 11:04

Patrioticcow


People also ask

How to get data from JSON file in jQuery?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

How to fetch data from JSON file and display in html table using jQuery?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

How to fetch data from JSON file in JavaScript?

We can read this JSON data in JavaScript using the import statement this way: <! ---./index. js--> import data from './data.

How to get data from JSON?

Any JSON data can be consumed from different sources like a local JSON file by fetching the data using an API call. After getting a response from the server, you need to render its value. You can use local JSON files to do an app config, such as API URL management based on a server environment like dev, QA, or prod.


2 Answers

Or if your data return was fieldname value pairs like:

{"firstname" : "John", "lastname" : "Doe"}

you could do it like:

$.getJSON('url_to_file', function(data) {
    for (var i in data) {
        $('input[name="'+i+'"]').val(data[i]);
    }
});
like image 88
William W. Doyle Avatar answered Sep 17 '22 05:09

William W. Doyle


Could you take a look at the JQuery loadJSON plugin on the http://code.google.com/p/jquery-load-json/ ? On the page http://code.google.com/p/jquery-load-json/wiki/WorkingWithFormElements is explained how this plugin load JSON object into the form. You can also find one live example here http://jquery-load-json.googlecode.com/svn/trunk/edit.html?ID=17. I think that this is exactly what you need. Just create empty form and load json in the form using the following code:

$('form').loadJSON(data);

This plugin handles all form elements such as check boxes, select list radio buttons etc. If you use it you will need to download plugin from the http://code.google.com/p/jquery-load-json/source/browse/trunk/scripts/jquery.loadJSON.js.

like image 43
Jovan MSFT Avatar answered Sep 17 '22 05:09

Jovan MSFT