Codeigniter jquery's自动完成不工作,但没有错误


codeigniter jquery's autocomplete not working but no error

我是javascript新手,我试着遵循这个教程。

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

这是我的控制器代码:
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,但是none我的echo是工作的:

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/methodsource: (JavaScript),但我不能这样做,因为生成的路由将有双控制器(index.php/absensi/absensi/autocomplete),所以我删除了controller,只是使用方法(source: "absensi")

我相信你使用source选项不正确。来自文档:

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

如果你想让source指向你的autocomplete函数,你需要给你的控制器名称,就像他们在你链接的演示中那样:

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

模型

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()中,而不是放在model..

这就是答案:

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

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

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