我已经成功创建了实时搜索,但现在我想在搜索后从可用建议中选择任何一个建议并将其显示为html的输入类型标记。我使用jquery结合codeigniter。所以,下面是包含jquery的控制器,模型和视图的代码。
控制器/ users.php:
public function live_search()
{
$search= $this->input->post('customer_name');
$query = $this->User_model->getCustomer($search);
echo json_encode ($query);
}
模型/ User_model.php:
function getCustomer($search)
{
$this->db->select('customer_name');
$this->db->like('customer_name', $search, 'both');
$this->db->from('customer_details');
$query = $this->db->get();
return $query->result();
}
查看:
<script>
$(document).ready(function()
{
$("#customer_name").keyup(function()
{
if($("#customer_name").val().length>2)
{
$.ajax({
type: "post",
url: "http://127.0.0.1/atop/live_search",
cache: false,
data:'customer_name='+$("#customer_name").val(),
success: function(response){
$('#finalResult').html("");
var obj = JSON.parse(response);
if(obj.length>0)
{
try
{
var items=[];
$.each(obj, function(i,val)
{
items.push($('<ul/>').text(val.customer_name));
});
$('#finalResult').append.apply($('#finalResult'), items);
}
catch(e)
{
alert('Exception while request..');
}
}
else
{
$('#finalResult').html($('<ul/>').text("No Data Found"));
}},
error: function()
{
alert('Error while request..');
}
});
}
return false;
});
});
</script>
<div class="col-md-12">
<label>Customer Name:</label><br>
<input type="text" name="customer_name" id="customer_name">
<ul id="finalResult"></ul>
</div>
所以,我怎么能得到它?