我正在尝试编写一些基于来自 Sharepoint WebServices getList 的响应构建 html 控件的 javascript。我将控件存储在一个数组中。构建数组后,长度为 0,直到我执行警报,然后它变成正确的数字。
var controls = [];
$(document).ready(function() {
// Make call to WebServices to retrieve list data
$().SPServices({
operation: "GetList",
listName: qs["list"],
completefunc: parseList
});
console.log(controls.length); // this outputs 0
alert("This has to be here to work."); // this has to be here, no idea why
console.log(controls.length); // this outputs 6
for (var i=0; i<controls.length; i++) {
controls[i].addControl();
}
});
function parseList(xData,status) {
$(xData.responseXML).find("Field").each(function() {
if ($(this).attr("ID") && $(this).attr("SourceID") != "http://schemas.microsoft.com/sharepoint/v3") {
if ($(this).attr("Type") == "Text") {
controls.push(new Textbox(this));
} else if ($(this).attr("Type") == "Choice" && $(this).attr("Format") == "Dropdown") {
controls.push(new Dropdown(this));
} else if ($(this).attr("Type") == "Choice" && $(this).attr("Format") == "RadioButtons") {
controls.push(new RadioButtons(this));
} else if ($(this).attr("Type") == "MultiChoice") {
controls.push(new MultiChoice(this));
} else if ($(this).attr("Type") == "Boolean") {
controls.push(new Boolean(this));
}
}
});
}
警报似乎是唯一可以controls.length
正常工作的东西。我只能认为这是某种范围界定问题。任何见解都值得赞赏。