gpt4 book ai didi

javascript - 注释代码会改变先前代码的行为

转载 作者:行者123 更新时间:2023-11-28 16:34:17 25 4
gpt4 key购买 nike

伙计们 - 我不知道是什么导致我的代码在这里中断,我希望你们能帮助找出原因!您在下面看到的函数从 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");
}
}
});
}

alt text

正如你所看到的,console.log(tier) 在应该显示 2 的时候却显示了 NaN。真正奇怪的是,如果我注释掉包含 tempstring += "+"; 的 for 循环。 console.log 按您的预期工作。我能看到的唯一关系是 for 循环使用 p < tier 但这不应该对之前的 tier 变量声明产生任何影响,对吧?

哦,我想这也是不言而喻的 tempstring += ""+ tier;也坏了^^

谢谢!

最佳答案

大量代码,没有 HTML 转储,但我有一个猜测。

第一次尝试似乎运行良好,但是:

var currenttier = parseInt(thisRow.find("td.Tier").text());

之后会杀了你,因为你在每一层添加了一个 +:

console.log(parseInt('++ 1')); // NaN

您必须去掉加号,否则您的号码将无法解析。

关于javascript - 注释代码会改变先前代码的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com