Javascript jquery自动完成不显示数组的值
您好,我正在使用jquery autocomplete和codeigniter。我的reslut数组来自数据库,我正在使用json_encode()发回数组。但是作为回应,我不能在搜索中显示值,但我得到了空的reslut,这意味着我得到了值,但我不能显示,但值正在到来我可以看到空的lis,我的数据数组如下Javascript jquery自动完成不显示数组的值,javascript,php,jquery,arrays,codeigniter,Javascript,Php,Jquery,Arrays,Codeigniter,您好,我正在使用jquery autocomplete和codeigniter。我的reslut数组来自数据库,我正在使用json_encode()发回数组。但是作为回应,我不能在搜索中显示值,但我得到了空的reslut,这意味着我得到了值,但我不能显示,但值正在到来我可以看到空的lis,我的数据数组如下 [{"countryid":"1","countryname":"Afghanistan"},{"countryid":"2","countryname":"Albania"},{"count
[{"countryid":"1","countryname":"Afghanistan"},{"countryid":"2","countryname":"Albania"},{"countryid":"3","countryname":"Algeria"},{"countryid":"4","countryname":"American Samoa"},{"countryid":"5","countryname":"Andorra"}]
public function search_countries($string)
{
$this->db->like('countryname', $string);
$query = $this->db->get('countries');
return $query->result_array();
}
我的模型函数是这样的
[{"countryid":"1","countryname":"Afghanistan"},{"countryid":"2","countryname":"Albania"},{"countryid":"3","countryname":"Algeria"},{"countryid":"4","countryname":"American Samoa"},{"countryid":"5","countryname":"Andorra"}]
public function search_countries($string)
{
$this->db->like('countryname', $string);
$query = $this->db->get('countries');
return $query->result_array();
}
我的控制器功能如下
public function search_countries()
{
$string = $this->input->post('countryname');
$data = $this->user_m->search_countries($string);
$this->output->set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
}
这里是jquery函数
$( ".country" ).autocomplete({
source: function( request, response ) {
$.post("signup/search_countries", {countryname: request.term}, function(data){
response(data, function( data ) {
return {
label: data.countryid,
value: data.countryname
}
});
});
}
});
这不应该是:
label: data.countryid,
value: data.countryName
作为:
根据您收到的实际json响应(将
Name
更改为countryName
中的Name
)。我只给出js的一部分
$( ".country" ).autocomplete({
source: function( request, response ) {
$.post("signup/search_countries", {countryname: request.term}, function(data){
response($.map( data, function( item ) {
return {
label: item.countryname,
value: item.countryid
}
}));
});
}
});
你能试试这个吗?@ahmadgulzar那么你应该在上面的问题中纠正它,因为它仍然显示contryName,而你的json响应显示countryname。@ahmadgulzar你在使用firebug吗?你能检查一下,你是否收到了来自服务器的任何响应吗?同时尝试执行类似于
console.log(data)
的操作,这样我们就可以确定它是否只是一个字符串,或者是否实际转换为JS对象。是的,我正在接收响应。当我在alert()中获得值时,它显示如下[object object],您需要使用正确的键,就像返回json一样。keys是correct@cubuzoa,并且您是否检查了firebug上的响应数据?这是正确的,你没有得到任何js错误的权利吗?谢谢,但它不工作。它以空列表的形式显示结果。我也可以悬停,但这些是空白元素。请检查您的响应json键。好的,我更新了代码。请你再试一次好吗。我忘了使用mapThank@cubuzoa,它起作用了,但我纠正了一些错误。它成功了