我有一个函数,它返回与按城市名称排序city_id
关联的城市名称列表。
我想在选择框中显示它们。在Firefox(23.0.1)中,即使有订单也可以正常工作。但是在IE(10)和铬(29.0.1547.66 m)的情况下,顺序不正确。我正在使用PHP和zend框架。我的代码:
$cities = $countryCityModel->getCities($country);
print json_encode(array('status'=>'Success',
'data'=>$cities)
);
public function getCities($countryId){
if(!$countryId)
return false;
$mdb = $this->getOldDbAdapter();
$sql = $mdb->select()
->from('cities', array('city_id','city'))
->where('country_id = ?', $countryId)
->order("city");
return $mdb->fetchPairs($sql);
}
$.ajax({
url : baseUrl + '/lead/index/get-cities',
dataType : 'json',
data : {
country:country_id
},
beforeSend : function(){
$("#holder_popup").show();
},
complete: function(){
$("#holder_popup").hide();
},
success : function(res) {
var sbuOptions = "<option value=''>--SELECT--</option>"
if(res.status == 'Success'){
for(i in res.data){
sbuOptions += "<option value='"+i+"'>"+res.data[i]+"</option>";
}
$("#city").html(sbuOptions);
}else{
$("#city").html(sbuOptions);
alert(res.msg);
}
},
error : function(jqXHR, exception) {
}
});
返回值如下所示:
{
"status":"Success",
"data":{
"53029":"DURRES",
"53331":"ELBASAN",
"40239":"FIER",
"16235":"KAMEZ",
"42191":"KAVAJE",
"41375":"KUKES",
"53581":"PESHKOPI",
"57686":"SHIJAK",
"56756":"SHKODER",
"4496":"TIRANA",
"41342":"VLORE",
"19454":"VORE"
}
}
请帮帮我,如何解决这个问题?
你能把
你的代码更新成这个吗
public function getCities($countryId)
{
$resultArrary = array();
if($countryId)
{
$mdb = $this->getOldDbAdapter();
$sql = $mdb->select()
->from('cities', array('city_id','city'))
->where('country_id = ?', $countryId)
->order("city");
$result = $mdb->fetchAll($sql);
foreach($result as $key => $city )
{
$resultArrary[$key]['id'] = $city['city_id'];
$resultArrary[$key]['city'] = $city['city'];
}
}
return $resultArrary;
}
for(i in res.data)
{
cityData = res.data[i];
sbuOptions += "<option value='"+cityData.id+"'>"+cityData.city+"</option>";
}
似乎 chrome 是使用索引自动对 json 对象进行排序的。
裁判:
Chrome和IE自动对JSON对象进行排序,如何禁用?
https://code.google.com/p/chromium/issues/detail?id=37404
您在第 12
行缺少 ASC 或 DESC
->order("city ASC");