4

我的 ajax自动完成确实会从服务器返回国家,但无论我输入什么,它总是显示所有国家。将 request.term 发送到控制器到模型有问题,但我不知道它是什么。非常感谢任何提示!

html:

<input type='text' name='location_search' id='location_search' placeholder='Type Location' >

js:

$(function(){
    var $sfield = $('#location_search').autocomplete({
        source: function(request, response){
            var url = "<?php echo site_url('autocomplete/control_areas'); ?>";
              $.post(url, {data:request.term}, function(data){
                response($.map(data, function(countries) {
                    return {
                        value: countries.name_en
                    };
                }));
              }, "json");  
        },
        minLength: 2,
        autofocus: true
    });
});

控制器:

function control_areas(){
    $this->load->model('autocomplete_model');
    $term = $this->input->get('term', TRUE);
    $countries = $this->autocomplete_model->get_areas($term);
    echo json_encode($countries);
}

模型:

function get_areas($term){
    $this->db->where('name_en', $term);
    $query = $this->db->get('countries');
    return $query->result(); 
}
4

2 回答 2

3

问题出在您的模型中,应该是这样的:

function get_areas($term){
    $this->db->like('name_en', $term, 'after');
    $query = $this->db->get('countries');
    return $query->result(); 
}
于 2013-11-26T13:17:25.287 回答
1

简单的小事:

$this->db->like

代替

$this->db->where
于 2013-10-30T07:20:13.017 回答