我不明白爲什麼我的全局變量workRegionCode
設置不正確。爲什麼沒有設置全局變量值?
在功能getWorkRegion()
,讓Ajax回調之後,它嘗試設置workRegionCode
全局變量。 (在功能setFirstIndexWorkRegionCode()
內)。
期望值像401或123等
但在setFirstIndexWorkRegionCode()
輸出警報然後當getMachines()
被調用時,全局變量是workRegionCode
:(undefiend
這個js從開始的window.onload()
(請忽略這些日本JSON鍵值和日本少數變量他們是無害的。)
編號:
var workRegionDropdown = document.getElementById("workRegionDropdown");;
var machineDropdown = document.getElementById("machineDropdown");
//this is the global variable with problem.....
var workRegionCode;
//INIT
window.onload = function() {
getWorkRegions();
// alert("before: " + window.workRegionCode);
getMachines();
// alert("after: " + window.workRegionCode);
}
function addWorkRegionToDropdown(jsonObject, dropdown) {
for(var i=0, j=jsonObject.length; i<j; i++) {
var option = document.createElement("option");
option.text = jsonObject[i].作業區コード + ":" + jsonObject[i].作業區名漢字;
option.value = jsonObject[i].作業區コード;
dropdown.options.add(option);
}
}
function addMachineToDropdown(jsonObject, dropdown) {
for(var i=0, j=jsonObject.length; i<j; i++) {
var option = document.createElement("option");
option.text = jsonObject[i].機械名;
option.value = jsonObject[i].機械名;
dropdown.options.add(option);
}
}
function getMachines() {
//!!!!!!!!!!! workRegionCode is undefined.. why?!?!?!
alert("inside of getMachines() ==> " + window.workRegionCode);
var ajaxRequest = new XMLHttpRequest();
ajaxTimeout = setTimeout(function() {timeoutAjax(ajaxRequest, "showMessage")}, "5000");
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 ) {
clearTimeout(ajaxTimeout);
switch (ajaxRequest.status) {
case 200:
var jsonOut = JSON.parse(ajaxRequest.responseText);
addMachineToDropdown(jsonOut.機械, machineDropdown);
break;
default:
document.getElementById("showMessage").innerHTML = ajaxRequest.responseText;
}
}
}
var aMonthAgo = new Date();
with(aMonthAgo) {
setMonth(getMonth() - 1)
}
aMonthAgo = aMonthAgo.getYYYYMMDD();
var 終了日 = "29991231";
var url = "../resources/machine/list/" + window.workRegionCode + "/" + aMonthAgo + "/" + 終了日;
ajaxRequest.open("GET", url, true);
ajaxRequest.send(null)
}
function getWorkRegions() {
var ajaxRequest = new XMLHttpRequest();
ajaxTimeout = setTimeout(function() {timeoutAjax(ajaxRequest, "showMessage")}, "5000");
ajaxRequest.onreadystatechange = function() {
if(ajaxRequest.readyState == 4 ) {
clearTimeout(ajaxTimeout);
switch (ajaxRequest.status) {
case 200:
var jsonOut = JSON.parse(ajaxRequest.responseText);
//set global variable workRegionCode
setFirstIndexWorkRegionCode(jsonOut);
addWorkRegionToDropdown(jsonOut.作業區, workRegionDropdown);
default:
document.getElementById("showMessage").innerHTML = ajaxRequest.responseText;
}
}
}
var url = "../resources/workshop/list";
ajaxRequest.open("GET", url, true);
ajaxRequest.send(null)
}//end getWorkRegions()
function setFirstIndexWorkRegionCode(jsonString) {
//here I set the value to work region code!
window.workRegionCode = jsonString.作業區[0].作業區コード;
alert("作業區コード: " + window.workRegionCode);
}
是的我沒有「窗口」開始,但我讀了一篇文章,說全局變量隱式地是窗口對象的屬性。此外日文變量是好的,另一個帶有jap chars的js文件工作得很好。 – 2011-02-25 07:26:48