尝试引用两个单独的 JSON (.txt) 文件,其中第二个依赖于第一个中的值。成品应该是一个h4标题,下面有一个“Spring”和“(变量)老师的姓氏”。相反,我每次都会列出所有标题 h4-s,然后是增加(增加一个)数量的值。结果示例:
<first h4>
<second h4>
<third h4>
spring
------
name associated with first h4
<first h4>
<second h4>
<third h4>
spring
------
name associated with first h4
spring
------
name associated with second h4
<first h4>
<second h4>
<third h4>
spring
------
name associated with first h4
spring
------
name associated with second h4
spring
------
name associated with third h4
我认为这可能与 getJSON 是异步的有关……这是 javascript:
<script>
$(document).ready(function() {
$.getJSON(("degree.txt"), function(data) {
var courselisting = "";
for (var i in data) {
var teacherfirst = data[i].year.spring.firstname;
var teacherlast = data[i].year.spring.lastname;
courselisting+= '<div class="grid">';
courselisting+= '<div class="col-1-1"><h4 class="ci-header ci-round">' + data[i].course + ', ' + data[i].credits + ' Credit Hour(s)</h4></div>';
$.getJSON(( "/programs/course-information/faculty/" + teacherfirst + teacherlast + ".txt"), function(data) {
var lastname = data.lastname;
var url = data.url;
courselisting+= '<div class="col-1-4"><div class="col-1-3"><p class="small head">Spring<br></p><p class="small"><a id="" class="hlight" href="' + url + '" target="new">' + lastname + '</a></p></div></div></div>';
document.getElementById("schedule-container").innerHTML+=courselisting;
});
};
});
});
</script>
感谢你们提供的任何帮助!