我正在尝试从JSON文件中获取值,数据以数组的形式存储。这是代码:
JS:
var initDate = new Date("7/9/2013");
var diffDays = 0;
var timeS;
function taqvimSingleDate() {
// Calculation of Dates difference
var currDate = new Date();
diffDays = Math.floor((currDate.getTime() - initDate.getTime()) / (1000 * 60 * 60 * 24));
$.getJSON('allData.json', function(data) {
if (data != '' || data != undefined) {
var addTohtml = '';
addTohtml += '<td>' +data[diffDays].date_g+ '</td>';
$('#singleDate').html(addTohtml);
// Display appropriate time according to location (01 or 02 etc.)
switch(getCity) {
case '01': $('#sahari').html(data[diffDays].sahar_1);
timeS = data[diffDays].sahar_1;
break;
case '02': $('#sahari').html(data[diffDays].sahar_2);
timeS = data[diffDays].sahar_2;
break;
case '03': $('#sahari').html(data[diffDays].sahar_3);
timeS = data[diffDays].sahar_3;
break;
case '04': $('#sahari').html(data[diffDays].sahar_4);
timeS = data[diffDays].sahar_4;
break;
}
} else {alert("Database is undefined/empty!");}
});
}
alert(timeS);
元素的结果#sahari
正确显示(根据getCity的值),但问题是我的timeS
变量未定义,我被困在这里。如何#sahari
正确地将值传递给timeS
变量?