【问题标题】:jquery autocomplete not working with codeigniterjquery自动完成不适用于codeigniter
【发布时间】:2015-06-21 14:14:11
【问题描述】:

首先,是的,我知道还有其他类似的问题,我已经尝试了那里的解决方案,但没有运气。

我正在使用 PHP、codeigniter 和 jquery UI 自动完成功能。我正在关注this 教程,但它不起作用。

我的模特:

class Tags_model extends Model{
  function get_tag($q){   
    $db = mysqli_connect("localhost","root","","km_portal") or die("Could not connect");
    $query = mysqli_query($db,"SELECT * FROM resource WHERE tags LIKE '%".$q."%'");

    if(mysqli_num_rows($query) > 0){
        while($row = mysqli_fetch_array($query)){           
            $new_row['label']= $row['tags'];
            $new_row['value']= $row['tags'];
            $row_set[] = $new_row; //build an array
        }
       echo json_encode($row_set); //format the array into json       
    }}}

我的看法:

<?php echo form_open('search/do_search');?>
    <input type="text" name="search" id="tags" placeholder="Search tags"/>
    <input type="submit" value=">>" />
    <span id="noMatches"></span>
</form>

使用javascript:

<script type="text/javascript">
$(jQuery(document).ready(function(){
    $("#tags").autocomplete({
        source: "search/get_tags",
        response: function(event,ui){
            if (ui.content.length === 0) {
                $("#noMatches").html("No matches");
            } else {
                $("#noMatches").empty();
            }
        }
    });
}));

最后,我的控制器:

function get_tags(){
   $this->load->model('tags_model');

    if(isset($_GET['term'])){
        $q = strtolower($_GET['term']);
        $this->tags_model->get_tag($q);
    }}

当我尝试运行它时检查 Chrome 控制台,错误似乎是参数 'term' 将自身附加到 url 使其成为:localhost/km/index.php/search/get_tags?term=a。我收到错误 404(未找到)

任何帮助将不胜感激。我已经坚持了一个多月(这是我第一次使用codeigniter)。

谢谢!

编辑:使用以下js:

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

【问题讨论】:

  • 你添加jquery.ui.js了吗?
  • 您好,是的,我已经链接到它了。感谢您提醒我提及这一点。

标签: javascript php jquery codeigniter autocomplete


【解决方案1】:

您遇到了路由问题,我猜您的搜索网址是

http://domain.com/search/get_tags?term=blah

我在这里看到了问题:

<?php echo form_open('search/do_search');?>
    <input type="text" name="search" id="tags" placeholder="Search tags"/>
    <input type="submit" value=">>" />
    <span id="noMatches"></span>
</form>

表单的路径应该是这样的:/search/do_search

要创建绝对路径,否则您将获得相对路径

编辑:只是为了帮助你,当我使用自动完成功能时,我曾经在谷歌中检查 url,在你的情况下为 http://domain.com/search/get_tags?term=blah,是否正确检索 json,以调试问题

【讨论】:

  • 你好!感谢您的建议,但我已经这样做了,它并没有改变任何东西。仍然无法让自动完成功能工作:(
  • 你能告诉我你在浏览器控制台中收到了什么错误消息吗?
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2020-05-05
  • 2016-03-21
  • 2015-08-05
  • 1970-01-01
  • 2011-05-13
相关资源
最近更新 更多