codeigniter jquery的自动完成功能不起作用但没有错误

时间:2013-04-01 13:15:49

标签: php javascript jquery codeigniter autocomplete

我是javascript的新手,我尝试按照this教程。

我有一个文本框(输入),我想通过从MySQL加载一些数据来使用jQuery的自动完成。

这是我的控制器代码:

public function autocomplete() {
    if($this->input->post('txt_nama'))
        $this->absensi_m->get_umat($this->input->post('txt_nama'));

    /*if (isset($_GET['term'])){
        $q = strtolower($_GET['term']);
        $this->absensi_m->get_umat($q);
    }*/
}

这是我的模特:

public function get_umat($word) {
    $this->db->select('nama', 'tanggal_lahir');
    $this->db->like('nama', $word);
    $query = $this->db->get('msumat');

    if($query->num_rows() > 0)
    {
        foreach($query->result_array() as $row)
        {
            $new_row['label'] = htmlentities(stripslashes($row['nama']));
            $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
            //build array
            $row_set[] = $new_row;
        }
        echo json_encode($row_set);
    }
}

这是我的javascript:

<script type="text/javascript">
        $(function(){
            $("#txt_nama").autocomplete({
                source: "autocomplete"
            });
        });
    </script>

我尝试使用firefox的firebug和GC的开发工具检查javascript,这就是我得到的:

<input type="text" id="txt_nama" name="txt_nama" class="ui-autocomplete-input" autocomplete="off">

请注意,自动填充 关闭。我想这是问题所以我试图通过添加此代码来启用它:

$(document).ready(function() {
            $("#txt_nama").attr("autocomplete", "on");
        });

当我添加此代码时,自动填充元素在,但自动填充功能仍然 正在工作。

我也尝试使用echo,但我的echo的 none 正在运行:

if($query->num_rows() > 0)
    {
        echo num_rows();
        echo 'a';

        foreach($query->result_array() as $row)
        {
            $new_row['label'] = htmlentities(stripslashes($row['nama']));
            $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
            //build array
            $row_set[] = $new_row;
        }
        echo json_encode($row_set);
        //return row_set;
    }

我错过了什么?

注意: 我只是想知道Routes,它是否与此错误有关?因为通常情况下,人们在controller/method(JavaScript)中使用source:,但我不能这样做,因为生成的路由会有双控制器(index.php/absensi/absensi/autocomplete),所以我删除controller并使用方法(source: "absensi"

3 个答案:

答案 0 :(得分:2)

我相信您错误地使用了source选项。来自docs

  

当使用字符串时,Autocomplete插件希望该字符串指向将返回JSON数据的URL资源。

如果你想让source指向你的autocomplete功能,你需要提供你所控制的控制器名称,就像你在链接的演示中一样:

source: "birds/get_birds" // path to the get_birds method

答案 1 :(得分:1)

<强>模型

public function get_umat($word) {
    $this->db->select('nama', 'tanggal_lahir');
    $this->db->like('nama', $word);
    $query = $this->db->get('msumat');

    if($query->num_rows() > 0)
    {
        foreach($query->result_array() as $row)
        {
            $new_row['label'] = htmlentities(stripslashes($row['nama']));
            $new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
            //build array
            $row_set[] = $new_row;
        }
        return $row_set;
    }
}

<强>控制器:

public function autocomplete() {
    if($this->input->post('txt_nama'))
        echo json_encode($this->absensi_m->get_umat($this->input->post('txt_nama')));

}

您应该将echo json_encode()放在autocomplete()中,而不是放在模型中。

答案 2 :(得分:0)

这就是答案:

  1. 不要使用echo,它会破坏jquery。

  2. 使用if (isset($_GET['term']))代替$this->input->post()

  3. 在jquery

  4. 中使用source: "<?php echo site_url('absensi/autocomplete'); ?>"