我不明白为什么我的全局变量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