【发布时间】:2017-11-22 01:04:33
【问题描述】:
我正在尝试使用 jQuery 构建表格行并将其附加到现有表格。在我的行构建函数中,我需要基于 $.each 循环内的 theJSON.EmployeeID 的当前值进行另一个 Ajax 调用。
内部$.getJSON 执行,检索有效的JSON,并触发它的回调,但包含data.Name 的td 没有连接到trString! trString 甚至没有收到空的 td,似乎该行根本没有被执行。我是否遗漏了一些明显的东西,或者我滥用了$.getJSON,还是什么?
//This call succeeds and returns valid JSON
$.getJSON("ajax/queries.php?q=licenseMain&oId=" + oId + "&sId=" + sId + "&eId=" + eId + "&inUse=" + inUse + "&page=1", function (licensedata) {
buildLicenseTable(licensedata);
});
function buildLicenseTable(trArray) { //Build table row-by-row
var trString = "";
$.each(JSONforTable, function (key, theJSON) { //Build single table row
trString += "<tr>";
trString += "<td>" + theJSON.LicenseID + "</td>";
trString += "<td>" + theJSON.LicenseNumber + "</td>";
trString += "<td>" + theJSON.LicenseType + "</td>";
//The inner $.getJSON call
$.getJSON("ajax/queries.php?q=employee&eID=" + theJSON.EmployeeID, function (data) {
console.log("*---Callback executing!---*");
trString += "<td>" + data.Name + "</td>"; //<----This line doesn't execute
});
trString += "</tr>";
$("#bigtable > tbody:last").append(trString);
});
}
【问题讨论】:
-
您的脚本中 JSONforTable 是如何定义的?
标签: javascript jquery ajax html-table concatenation