2 つの個別の JSON (.txt) ファイルを参照しようとしていますが、2 番目のファイルは最初のファイルの値に依存しています。完成品は h4 タイトルで、下に「春」と「(可変) 先生の姓」が表示されます。代わりに、毎回リストされているすべてのヘッダー h4-s を取得し、その後に (1 つずつ) 値の数を増やしています。それがどうなるかの例:
<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>
皆さんが提供できる助けをありがとう!