我正在使用codeigniter中的ajax为自动完成选择框编写代码。代码已成功执行,但输出的问题并非如我所愿。我希望列表进入文本框,输出作为列表出现在文本框之外。这是我的代码:
控制器:
public function auto()
{
$country = $this->input->post('country');
$this->load->model('m_details');
$query = $this->m_details->get_auto($country);
foreach($query->result() as $row)
echo "<li>$row->country</li>";
}
型号:
public function get_auto($country)
{
$this->load->database();
$this->db->select('country');
// $this->db->from('global_geo_country');
$this->db->like('country', $country,'both');
//$this->db->from('global_geo_country');
return $this->db->get('global_geo_country');
}
查看:
function ajaxSearch()
{
var input_data = $('#country').val();
if (input_data.length === 0)
{
$('#suggestions').hide();
}
else
{
var post_data = {
'country': input_data,
'<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
};
$.ajax({
type: "POST",
url: "<?php echo base_url();?>test/auto",
data: post_data,
success: function(data) {
// return success
if (data.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data);
}
}
});
}
HTML:
<div class="search">
<input name="country" id="country" type="text" onkeyup="ajaxSearch();">
<div id="suggestions">
<div id="autoSuggestionsList">
</div>
</div>
</div>
如何在文本框中显示输出?请指导我。