jquery autocomplete不显示数组的值

时间:2014-01-15 08:07:05

标签: javascript php jquery arrays codeigniter

嗨我正在使用带有codeigniter的jquery自动完成功能,我的reslut数组来自数据库,我使用json_encode()发回数组。但作为回应我无法在搜索中显示值,但我得到空的reslut,意味着我得到值,但我无法显示但值即将到来我可以看到空lis,我的数据数组是这样的< / p>

[{"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
              }
            });
        });
      }
    });

2 个答案:

答案 0 :(得分:2)

不应该这样:

            label: data.countryid,
            value: data.countryName

as:

            label: data.countryid,
            value: data.countryname

根据您收到的实际json回复(在Name中将name更改为countryName)。

答案 1 :(得分:0)

我只给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
              }
            }));
        });
      }
    });

你能试试吗?