我在codeigniter中有我的代码。阵列在组合框之外工作正常,但json对象按照chrome和其他浏览器中的id重新排序。我证明了一切,并找到了解决方案,但现在数组是对象对象。
代码是这样的:
观点:
$.ajax({
type: "POST",
dataType: "json",
url: "http://www.url.com/admin/modificar_contacto/get_clientes/",
success: function(nombres)
{
console.log(nombres);
var temp = [];
$.each(nombres,function(id,nombre)
{
temp.push({v:id,k:nombre});
console.log(temp);
});
temp.sort(function(a,b){
if(a.k > b.k){ return 1}
if(a.k < b.k){ return -1}
return 0;
console.log(temp);
});
$.each(temp,function(key, obj) {
$('#nombres').append('<option value="'+key+'">'+obj+'</option>');
});
}
控制器:
function get_clientes(){
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->contenido_admin_model->get_clientes()));
}
模特:
function get_clientes() {
$this -> db -> select('cliente_id,cliente_nombre');
$this->db->order_by("cliente_nombre", "asc");
$this->db->where('cliente_estado',0);
$query = $this -> db -> get('ad_cliente');
$clientes = array();
if ($query -> result()) {
foreach ($query->result_array() as $cliente) {
$clientes[$cliente['cliente_id']]=$cliente['cliente_nombre'];
}
return $clientes;
} else {
return FALSE;
}
}
非常感谢您的帮助。我不知道还能做什么。我坚持了几天......