7

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

4 Answers 4

14

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]);
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

$.each(data, function(key, val) { }); instead of for will be better
why would it be better? benchmarks i've seen show that traversing with the native for loops is faster, particularly with large objects.
ok, may be it's a little bit faster, but each syntax looks like a normal iterator compared to native for with some things like data[i] and if you are not using very large objects I think you should use each
While the OP's example uses input, it should be noted that the provided code won't hit textareas. I would change this to $('.form-class *[name="'+i+'"]').val(data[i]); - Where .form-class is the form you're wanting to target.
2

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.

Comments

1

You may also use $.get like below;

$.get('your_file.[php/json]',function(d){
    $("input[name='firstname']").val(d.data);
    $("input[name='firstname']").val(d.size);
},'json');

Comments

0

So, what's wrong with $.getJSON? It works fine:

$.getJSON("1.json", function(data) {
  $('input[name="firstname"]').val(data["data"]);
  $('input[name="lastname"]').val(data["size"]);
});

1 Comment

yeah, that works, but i was looking for a more shorter version of the code, because i might have 20 input boxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.