【问题标题】:Autocomplete in CodeIgniter is not working (jQuery UI )CodeIgniter 中的自动完成功能不起作用(jQuery UI)
【发布时间】:2015-01-02 06:49:17
【问题描述】:

jQuery 代码:

    $(function(){
      $("#birds").autocomplete({
        source: "http://localhost/codeigniter/learning/autocomplete"
      });
    });

查看:

    <label>Normal Autocomplete</label>
    <input type="text" id="birds" />

控制器:

public function autocomplete()
{
    if (isset($_GET['term']))
    {
        $q = strtolower($_GET['term']);
        $this->learning_model->get_bird_string($q);
    }
}

型号:

public function get_bird_string($q)
{
    $this->db->select('bird');
    $this->db->like('bird', $q);
    $query = $this->db->get('birds');
    if($query->num_rows > 0)
    {
      foreach ($query->result_array() as $row)
      {
        $row_set[] = htmlentities(stripslashes($row['bird']));
      }
      echo json_encode($row_set); 
    }
}

这是我在文本框搜索中用于正常自动完成建议的代码,我在视图中包含的 js 文件是:

<link href="<?php echo base_url()?>js/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo base_url()?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-ui.js"></script>

问题是代码不起作用。我做错了什么??

【问题讨论】:

  • 你试过直接在浏览器中localhost/codeigniter/learning/autocomplete吗?
  • 在您的文本框中输入任何内容后,您能否检查控制台是否有任何错误。
  • 它是否发送任何 ajax 请求?在您的浏览器控制台检查它
  • 控制台中没有错误...实际上我有另一个设置用于自动建议图像,最初两者(正常和带图像)都可以工作,但现在只有带图像的搜索和正常的搜索不再工作了

标签: php jquery codeigniter autocomplete


【解决方案1】:

我认为你的模型函数出错了,因为 Num_rows 是一个函数而不是变量,所以试试吧,

if($query->num_rows() > 0) //num_rows() is a function
{
    foreach ($query->result_array() as $row) {
         $row_set[] = htmlentities(stripslashes($row['bird']));
    }
    echo json_encode($row_set); 
} else { // you can use elese part if no data found
   echo json_encode(array());// return json message
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2013-12-23
    • 2018-07-25
    • 2020-10-26
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多