jQuery的:$ .getJSON排序的Chrome浏览器/ IE浏览器中的数据? [英] jQuery: $.getJSON sorting the data on Chrome / IE?

查看:187
本文介绍了jQuery的:$ .getJSON排序的Chrome浏览器/ IE浏览器中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用Ajax和jQuery的$ .getJSON其正常读取数据和prepared对象接收它的关联数组(ID => VAL)。有,但是,很烦人的排序问题。

I'm passing an associative array (id => val) using Ajax and receiving it with jQuery's $.getJSON which read the data properly and prepared the object. There is, however, very annoying sorting issue.

看来,在Chrome和IE浏览器中的数据变成由副阵列的ID部分排序。因此,如果阵列应该是(5 =>'XXX',3 =>'FFF),它实际上变成(3 =>​​FFF,5 =>'XXX')。在Firefox中正常工作,即不进行排序。

It appears that on Chrome and IE the data becomes sorted by the id part of the associate array. So if the array should be (5=> 'xxx', 3 => 'fff') it actually becomes (3 => 'fff',5=> 'xxx'). On FireFox it works as expected, i.e. not sorted.

任何想法?

推荐答案

似乎是最好的方法是避免关联数组的。当你想发送的联系人阵列只会将其发送作为两个单独的阵列 - 一键以及价值观之一。这里的PHP code做到这一点:

Seems the best way is to avoid associative arrays at all. When you want to send an associate array simply send it as two separate arrays - one of keys and one of values. Here's the PHP code to do that:

    $arWrapper = array();
    $arWrapper['k'] = array_keys($arChoices);
    $arWrapper['v'] = array_values($arChoices);
    $json = json_encode($arWrapper);

和简单的JavaScript code做任何你想用它

and the simple JavaScript code to do whatever you'd like with it

            for (i=0; i < data['k'].length; i++) {
                console.log('key:' + data['k'][i] + ' val:' + data['v'][i]);
            }

这篇关于jQuery的:$ .getJSON排序的Chrome浏览器/ IE浏览器中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆