【发布时间】:2019-08-01 11:35:49
【问题描述】:
我有一个小脚本,它使用函数 getJSON 将 json 字符串保存为 var。之后我想用内容创建一些 div。
为此,我为猫(类别)创建了一个 each。在第二个中,当 repo 适合猫时,它也应该显示。 但该脚本永远不会进入 .when 或 .each 函数。 但是使用 console_log() 我可以看到 getJSON 函数的正确响应。
var repo;
var cat;
$.getJSON("api.php?get_repos&release_id=" + $("#release").find(":selected").data('id'), function (
json) {
repo = json;
});
$.getJSON("api.php?get_cat", function (json) {
cat = json;
});
$.when(repo, cat).done(function(){
$.each(cat, function (i, j) {
$(".tab").append('<button class="tablinks" onclick="openCAT(event, \'' + j.cat_name + '\'">' + j.cat_name + '</button>');
$(".repos").append('<div id="' + j.cat_name + '" class="tabcontent"></div>');
$.each(repo, function (k, v) {
if (v.repo_cat == j.cat_id) {
$("#"+ j.cat_name).append('<div class="repo"></div><p><label><input name="3rdparties[]" type="checkbox" value="' +v.repo_id + '"> ' + v.repo_name +'</label> <a href="' + v.repo_homepage +'" target="_blank" class="link"><i class="icon ion-earth"> Homepage</i></a> <a href="' + v.repo_documentation +'" target="_blank" class="link"><i class="icon ion-university"> Documentation</i></a>');
$("#"+ j.cat_name).append('<div class="inside">' + v.repo_desc + '');
$("#"+ j.cat_name).append('<a href="#" onClick="brokenRepo(\'' + v.repo_id +'\');" class="link right"><i class="icon ion-flash-off"> Broken Repo</i></a></div></p></div><br />');
}
});
});
});
【问题讨论】:
-
我怀疑你想要:
var repoajax = $.getJSON...然后$.when(repoajax, catajax).done
标签: javascript jquery