image.png
image.png
JS:
$(document).ready(function(){
var xhr=null;
$('#benefitPerson').bind('input propertychange', function() {
if(xhr){
xhr.abort();//如果存在ajax的請求,就放棄請求
}
var inputText= $.trim(this.value);
if(inputText!=""){//檢測鍵盤輸入的內容是否為空,為空就不發出請求
xhr=$.ajax({
type: 'GET',
url: '<?php echo base_url();?>index.php/ajax/ajaxBenefit',
cache:false,//不從瀏覽器緩存中加載請求信息
//data: "keyword=" + inputText,
data: {keyword:inputText},
dataType: 'json',
success: function (json) {
if (json.length != 0) {//檢測返回的結果是否為空
var lists = "<ul>";
$.each(json, function (index,obj) {
lists += "<li class='listName' name='listName[]'"+' data-id='+obj.id+">"+obj.username+"</li>";//遍歷出每一條返回的數據
// lists = '<li class="syh" name="bank_name[]">'+obj.NAME+'</li>';
});
lists+='</ul>';
$("#searchBox").html(lists).show();//將搜索到的結果展示出來
$(".listName").click(function(){
$("#benefitPerson").val($(this).text());//點擊某個li就會獲取當前的值
$("#code").val($(this).attr('data-id'));
$("#searchBox").hide();
})
} else {
$("#searchBox").hide();
}
}
});
}else{
$("#searchBox").hide();//沒有查詢結果就隱藏搜索框
}
}).blur(function(){
//$("#searchBox").hide();//輸入框失去焦點的時候就隱藏搜索框
});
});
PHP:
public function ajaxBenefit(){
$this->load->model('User_model');
$keyword = $_GET['keyword'];
//$keyword = '張';
//$keyword='中國';//獲取輸入框的內容
$map['select']='id,username';
$map['from'] = 'member';
//$map['limit'] = '200';
//$map['where_arr']['is_group'] = 1;
$map['like'] = array('username',$keyword) ;
$list['list'] = $this->User_model->selectAllList($map);
//echo '<pre>';
//print_r($list['list']);die;
echo json_encode($list['list']);
}