伙计们 - 我不知道是什么导致我的代码在这里打破,我很乐意帮助找出原因!您在下面看到的函数从Web服务获取一行,并根据返回的数据构建一个表行。
function GetData(id, thisRow) {
if (id == 0) {
var tier = 0;
}
else {
var currenttier = parseInt(thisRow.find("td.Tier").text());
var tier = currenttier + 1;
}
//*** this console.log is affected by the for loop that appears later..
console.log(tier);
$.ajax({
type: "POST",
url: "F2.svc/GetChildTable",
data: id,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data, textStatus, xhr) {
var tempstring = "";
var temp = "";
if (data.Rows.length > 0) {
for (var i = 0; i < data.Rows.length; i++) {
tempstring += "<tr class = 'Show Row ItemID-" + data.Rows[i].ItemID + " ParentID-" + data.Rows[i].ItemParentID + " Tier-" + tier + "'>";
tempstring += "<td class='Tier'>";
//*** the loop below seems to break the tier variable declared up above..
for (var p = 0; p < tier; p++) {
tempstring += "+";
}
tempstring += " " + tier;
tempstring += "</td>";
tempstring += "<td class='ItemID'>";
tempstring += data.Rows[i].ItemID;
tempstring += "</td>";
tempstring += "<td class='ItemParentID'>";
tempstring += data.Rows[i].ItemParentID;
tempstring += "</td>";
tempstring += "<td class='ItemDateEntered'>";
tempstring += data.Rows[i].ItemDateEntered;
tempstring += "</td>";
tempstring += "<td class='ItemLastUpdated'>";
tempstring += data.Rows[i].ItemLastUpdated;
tempstring += "</td>";
tempstring += "<td class='ItemName'>";
tempstring += data.Rows[i].ItemName;
tempstring += "</td>";
tempstring += "<td class='ItemStatusID'>";
tempstring += data.Rows[i].ItemStatusID;
tempstring += "</td>";
tempstring += "<td class='ItemTasks'>";
tempstring += data.Rows[i].ItemTasks;
tempstring += "</td>";
tempstring += "<td class='ItemBlocks'>";
tempstring += data.Rows[i].ItemBlocks;
tempstring += "</td>";
tempstring += "<td class='ItemComments'>";
tempstring += data.Rows[i].ItemComments;
tempstring += "</td>";
tempstring += "<td class='ItemFilterID'>";
tempstring += data.Rows[i].ItemFilterID;
tempstring += "</td>";
tempstring += "<td class='ItemManHoursEst'>";
tempstring += data.Rows[i].ItemManHoursEst;
tempstring += "</td>";
tempstring += "<td class='ItemManHoursAct'>";
tempstring += data.Rows[i].ItemManHoursAct;
tempstring += "</td><td></td>"
}
tempstring += "</tr>";
var newRow = $(tempstring);
if (id == 0) {
$("table#Main > tbody").html(newRow);
}
else {
thisRow.after(newRow);
thisRow.addClass("ChildrenLoaded");
}
}
},
error: function (xhr, textStatus, ex) {
var response = xhr.responseText;
if (response.length > 11 && response.substr(0, 11) === '{"Message":' &&
response.charAt(response.length - 1) === '}') {
var exInfo = JSON.parse(xhr.responseText);
var text = "Message: " + exInfo.Message + "\r\n" +
"Exception: " + exInfo.ExceptionType; // + exInfo.StackTrace;
alert(text);
} else {
alert("error");
}
}
});
}
正如您所看到的,console.log(层)在应该显示时显示NaN。如果我注释掉包含tempstring + =“+”的for循环,那真的很奇怪。 console.log按预期工作。我能看到的唯一关系是for循环使用p&lt;等级,但这对先前的层变量声明不应有任何影响,对吧?
哦,我想也不用说tempstring + =“”+ tier;也被打破^^
谢谢!
答案 0 :(得分:2)
很多代码,没有HTML转储,但我有猜测。
它似乎在第一次尝试时运行良好,但是:
var currenttier = parseInt(thisRow.find("td.Tier").text());
之后杀死你,因为每层添加一个+
:
console.log(parseInt('++ 1')); // NaN
你必须删除加号,否则你的号码将无法解析。
答案 1 :(得分:2)
您的问题实际上不在循环中,但在此代码中:
var currenttier = parseInt(thisRow.find("td.Tier").text());
var tier = currenttier + 1;
这里发生的是你第一次解析“0”,它工作正常,因为“0”是一个int。
但是第二次,你正在解析“+ 1”,它没有给出有效的int,而是NaN。这可以通过将第一行更改为:
来解决var currenttier = parseInt(thisRow.find("td.Tier").text().replace(/\D/g, ''));