我的 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();
}