我在一些代碼中有一個$ .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