我在一些代码中有一个$ .getJSON调用,它似乎没有更新全局变量,我不知道为什么。 JSON数据正在加载OK,但出于某种原因,全局EventOptions数组未在for {}循环中更新。大写的注释涉及变量。有任何想法吗?谢谢Javascript匿名函数没有更新全局变量
function LoadMeasurementTypes() {
// Clear out EventOptions
EventOptions = ["..."];
// Push a couple on to EventOptions - THESE ADD OK
EventOptions.push("Temperature");
EventOptions.push("Pulse rate");
// Call json to get measurementTypes off the table
$.getJSON('./get-measurement-types.php', function (measurementTypeData) {
// Process each json element ([0].BP, [1].ph (Urine) etc.
for (var i = 0; i < measurementTypeData.length; ++i) {
// e is a storage variable to contain the current element
var e = measurementTypeData[i];
// Add the new measurement type
alert(e.measure_type); // OK works - we can see the measure_type
EventOptions.push(e.measure_type); // THESE ARE NOT BEING ADDED
}
} // end anonymous function
) // end get json call
EventOptions.push("Last one"); // THIS ONE IS BEING ADDED
}
是在该函数之外声明的EventOptions? – Fosco 2011-05-27 14:30:34