目前我有一个选择框,里面填满了地点(纽约、洛杉矶、拉斯维加斯等)。当我选择其中一个位置时,我会在另一个选择框中填写日期。我的 JSON 结果(根据 Firefox Firebug)是:
[[{"id":"1345","van":"17-09-2013"},
{"id":"1346","van":"01-10-2013"},
{"id":"1347","van":"15-10-2013"}],
[{"id":"1348","van":"05-11-2013"},
{"id":"1349","van":"19-11-2013"},
{"id":"1350","van":"03-12-2013"}]]
我相信这是 JSON 的正确格式。
当我使用 IE 并选择 New York 或 Las Vegas 时,它可以正常工作,但是当我选择 Los Angeles 时,它给我一个错误,提示找不到“id”。在 Firefox 中它确实有效。
这就是我调用 JSON 文件的方式:
$tpl = "<script>$('#locatie_id').change(function(){
$('#datum_id').empty();
$.getJSON('getLocationDates.php', {locatie_id:$('#locatie_id').val()}, function(data) {
var dateId = '". $datum ."';
$.each(data, function(index, array) {
var option = '';
option = '<option value=\"' + array[0]['id'] +'\"';
if(dateId == array[0]['id']){
option += ' selected';
}
option += '>'+ array[0]['van'] +' - '+ array[1]['van'] +' - '+ array[2]['van'] +'</option>';
$('#datum_id').append(option);
})
});
});</script>";
这是我的 getLocationDates.php:
<?php
$pdo = new PDO($dsn, $username, $password);
$rows = array();
if(isset($_GET['locatie_id'])) {
$query = "
SELECT locaties_datum.id, locaties_datum.cursus_id, locaties_datum.locatie_id, locaties_datum.van, locaties_datum.code, locaties.plaats, locaties.link
FROM locaties, locaties_datum WHERE 1 = 1
AND locaties_datum.locatie_id = locaties.locatie_id
AND locaties_datum.van > '". strtotime("now") ."'
AND locaties.active = '1'
AND locaties_datum.cursus_id = '9'
AND locaties.locatie_id = ?
GROUP BY locaties_datum.code ORDER BY locaties_datum.van ASC ";
$stmt = $pdo->prepare($query);
$stmt->execute(array($_GET['locatie_id']));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$index = 0;
$list = array();
foreach($rows as $dk => $dv){
//if($dv['van'] > strtotime("now")){
$query2 = "
SELECT locaties_datum.id as id, locaties_datum.van, locaties_datum.code, locaties_datum.cursus_id
FROM locaties_datum WHERE 1=1
AND locaties_datum.code = '". $dv['code'] ."'
GROUP BY locaties_datum.van
ORDER BY locaties_datum.van ASC ";
$stmt = $pdo->prepare($query2);
$stmt->execute(array($_GET['locatie_id']));
$dList = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(is_array($dList)){
$iSub = 0;
foreach($dList as $cdmValue){
//if($dv['van'] != $cdmValue['van']){
$list[$index][$iSub]['id'] = $cdmValue['id'];
$list[$index][$iSub]['van'] = $cdmValue['van'];
$iSub++;
//}
}
}
$index++;
//}
}
for($i = 0; $i < count($list); $i++){
$list[$i][0]['van'] = date('d-m-Y', $list[$i][0]['van']);
$list[$i][1]['van'] = date('d-m-Y', $list[$i][1]['van']);
$list[$i][2]['van'] = date('d-m-Y', $list[$i][2]['van']);
}
}
echo json_encode(array_values($list));
?>
有人能告诉我为什么它不能在 IE 中正常工作吗?