【发布时间】:2014-09-19 11:38:36
【问题描述】:
我想在调用 $(document).ready(function (){} 时导入一个 json 文件。我的 html 表单需要 json 文件中定义的属性。
我该怎么做?
【问题讨论】:
-
试试这个
$.getJSON('path', function(data){ console.log(data); })
标签: javascript jquery json
我想在调用 $(document).ready(function (){} 时导入一个 json 文件。我的 html 表单需要 json 文件中定义的属性。
我该怎么做?
【问题讨论】:
$.getJSON('path', function(data){ console.log(data); })
标签: javascript jquery json
试试这样:
$.getJSON( "path to json file", function( data ) {
var item = [];
$.each( data, function( key, val ) {
item.push( "<li id='" + key + "'>" + val + "</li>" );
});
$( "<ul/>", {
"class": "myclass",
html: item.join( "" )
}).appendTo( "body" );
});
【讨论】:
如何做到这一点的一个例子可能是:
<script type="text/javascript">
$(function(){
$.getJSON('names.json',function(data){
console.log('success');
$.each(data.employees,function(i,emp){
$('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
});
}).error(function(){
console.log('error');
});
});
</script>
【讨论】: