我一直试图显示搜索关键词的建议,但它没有显示任何建议。与此同时,我使用搜索表单按钮提交搜索关键字以在另一页面上显示结果。并且该搜索按钮正在运行,但建议无法以某种方式加载。
这是我的控制者:
public function autocomplete() {
$this->load->model('Search');
$keyword = $this->input->post('s_keywords');
$data = $this->Search->getrow($keyword);
echo json_encode($data);
}
这是我的模特:
public function getrow($keyword){
$this->db->select('n_id, n_title, n_date, n_description');
$this->db->like('n_title',$keyword, 'after');
return $this->db->get('tbl_news')->result_array();
}
这是我的观点:
<form class="navbar-form navbar-left" role="search" method="POST" action=" <?php echo base_url() .'index.php/galle/search' ?>">
<div class="form-group input-group" id="search_bar" style="">
<input id="s_keywords" name="s_keywords" type="text" class="form-control search_bar" placeholder="Search for..." autocomplete="off">
<ul class="dropdown-menu txtcountry" role="menu" aria-labelledby="dropdownMenu" id="DropdownCountry"></ul>
<span class="input-group-btn">
<button id="search_key" class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
</span>
</div>
</form>
Javascript(main.js):
$(document).ready(function () {
$("#s_keywords").keyup(function () {
$.ajax({
type: "POST",
url: "http://localhost/site_new/index.php/galle/autocomplete",
data: {
keyword: $("#s_keywords").val()
},
dataType: "json",
success: function (data) {
if (data.length > 0) {
$('#DropdownCountry').empty();
$('#s_keywords').attr("data-toggle", "dropdown");
$('#DropdownCountry').dropdown('toggle');
}
else if (data.length == 0) {
$('#s_keywords').attr("data-toggle", "");
}
$.each(data, function (key,value) {
if (data.length >= 0)
$('#DropdownCountry').append('<li role="presentation" >' + value['n_title'] + '</li>');
//$('#DropdownCountry').append('<li role="presentation" ><a role="menuitem dropdownnameli" class="dropdownlivalue">' + value['n_title'] + '</a></li>');
});
}
});
});
$('ul.txtcountry').on('click', 'li a', function () {
$('#s_keywords').val($(this).text());
});
});
更新1
现在我获取了所有列数据而不是搜索关键字建议,如何根据关键字获取建议
更新2
只有当我输入奇怪的关键字时才会出现建议:例如:a,abc,当我把ab放弃时,abcd不起作用,abcd就像偶数关键字一样
答案 0 :(得分:0)
public function autocomplete() {
$this->load->model('Search');
$keyword = $this->input->post('s_keywords');
$data = $this->Search->getrow($keyword);
echo json_encode($data);
}
不应该将$ keyword设置为$ keyword = $ this-&gt; input-&gt; post(&#39; 关键字&#39;); 在ajax电话会议成功之后,我很想拥有
success: function (data) {
if (data.length > 0) {
$('#DropdownCountry').empty();
$('#s_keywords').attr("data-toggle", "dropdown");
$('#DropdownCountry').dropdown('toggle');
$.each(data, function (key,value) {
if (data.length >= 0)
$('#DropdownCountry').append('<li role="presentation" >' + value['n_title'] + '</li>');
//$('#DropdownCountry').append('<li role="presentation" ><a role="menuitem dropdownnameli" class="dropdownlivalue">' + value['n_title'] + '</a></li>');
});
}
else if (data.length == 0) {
$('#s_keywords').attr("data-toggle", "");
}
}