gpt4 book ai didi

json - 访问jquery模板中的json文件

转载 作者:行者123 更新时间:2023-12-01 04:21:55 24 4
gpt4 key购买 nike

我希望 json 位于 json 文件中,并希望在 jquery 模板中访问,我该怎么做才能感谢您的帮助

var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999" },
{ Name: "The Inheritance", ReleaseYear: "1976" }
];

var markup = "<li><b>${Name}</b> (${ReleaseYear})</li>";

/* Compile the markup as a named template */
$.template( "movieTemplate", markup );

/* Render the template with the movies data and insert
the rendered HTML under the "movieList" element */
$.tmpl( "movieTemplate", movies )
.appendTo( "#movieList" );

现在我正在尝试仅访问提交的名称,如何从 json 文件访问名称字段

$(document).ready(function(){

$.getJSON('dat.js', function(data) {
$( "#movieTemplate" ).tmpl( data[i].name).appendTo( "#movieList" );
});

});

想要访问该字段,但错误显示为 data[0] 未定义

最佳答案

使用jQuery.getJSON()加载json文件内容:

var markup = "<li><b>${Name}</b> (${ReleaseYear})</li>";

/* Compile the markup as a named template */
$.template( "movieTemplate", markup );

$.getJSON('movies.json', function(data) {

// 'data' is the json loaded and parsed from the file
$.tmpl( "movieTemplate", data )
.appendTo( "#movieList" );

});

关于json - 访问jquery模板中的json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com