我有触发变化的jQuery函数组合框,它调用一个PHP脚本,它带来的结果从数据库中按名称排序不是ID(表市),并创建一个JSON文件的ID和名称城市。 问题就来了$ .getJSON(URL数据)的功能,它检索所有的JSON恩codeD数据很好,但它似乎是由ID自动对数据进行排序
I have a combo box that triggers a jquery function on change, it calls a php script which brings the results from the database sorted by name not by id (table cities) and creates a json file with the id and the name of the city. The problem comes with a $.getJSON(url, data) function, it retrieves all the json encoded data fine, but it is seems to be sorting the data automatically by the id
例如,如果PHP页面生成
for instance if the php page generates
id name
3 Dania Beach
1 Miami
2 Weston
的jQuery的getJSON后,通过ID对其排序,
after jquery getJSON it sorts it by id
id name
1 Miami
2 Weston
3 Dania Beach
有没有一种方法来禁用排序?不然怎么能在preserve的顺序按名称?
Is there a way to disable sorting? or how can in preserve the order by name?
这是一个关联数组:顺序并不重要。下面的JSON对象是等价的:
This is an associative array: order does not matter. The following JSON objects are equivalent:
{
"3" : "Danie Beach",
"1" : "Miami",
"2" : "Weston"
}
{
"1" : "Miami",
"2" : "Weston",
"3" : "Danie Beach"
}
如果你需要一个排序,您应该代替JSON中嵌入一个数组:
If you need an ordering, you should instead embed an array within the JSON:
{
"beaches" : [
{"key" : "3", "value" : "Danie Beach"},
{"key" : "1", "value" : "Miami"},
{"key" : "2", "value" : "Weston"}
]
}
实例应用:
jQuery.ajax({
...
dataType: 'json',
success: function(data) {
jQuery.each(data.beaches, function(i, beach) {
alert(i+': beach['+beach.key+'] = '+beach.value);
});
}
});