jQuery如何将一些json记录加载到表单字段中? [英] jQuery how to load some json records into a form fields?
本文介绍了jQuery如何将一些json记录加载到表单字段中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个json文件:
{
data:Click,
size:Here}
和一个表单:
< form>
名字:< input type =textname =firstname/>< br />
姓氏:< input type =textname =lastname/>
< / form>
我想知道将多个json记录加载到多个表单元素的正确sintax是什么?类似于你想要修改的表单,并且这些值会排队到这些字段中。
我知道我可以使用:
$ getJSON('link_to_json_file'function(){
});
但我在这里卡住
谢谢
{firstname:John,lastname:Doe}
你可以做到这一点例如:
$。getJSON('url_to_file',function(data){
for(var i in data) {
$('input [name =''+ i +'''').val(data [i]);
}
});
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
解决方案
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]);
}
});
这篇关于jQuery如何将一些json记录加载到表单字段中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文