用json jquery多次自动完成

时间:2013-01-22 19:54:24

标签: php jquery json autocomplete jquery-autocomplete

我正在研究jquery多个自动完成,但是,当我输入内容时,所有项目都会被发布而不是匹配的项目。我的Javascript是

$('.tags').bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
    event.preventDefault();
    }
}).autocomplete({
    source : function(request, response) {
        $.getJSON($.cookie('base_url') + "js/getaddressbook.php", {
            term : extractLast(request.term)
        }, response);
    },
    search : function() {
        var term = extractLast(this.value);
        if (term.length < 2) {
            return false;
        }
    },

    focus : function() {
        return false;
    },
    select : function(event, ui) {
        var terms = split(this.value);
        terms.pop();
        terms.push(ui.item.value);
        terms.push("");
        this.value = terms.join("; ");
        return false;
    }
});

js / getaddressbook.php返回

  

[ “gunjan.soni”, “askhr”, “saurabh.burman”, “Aditi.Nehra”, “ithelpdesk”, “shipra.kwatra”, “gagandeep.manchanda”]

不知道我哪里出错。

下面是快照的样子

http://i.stack.imgur.com/SmRjX.png

请帮忙!

我的JS控制器具有

功能
Public function getaddressbook() {
        $this -> load -> model('common_model');
        $data = $this -> common_model -> addressbook();
        echo json_encode($data);
    }

common_model具有函数

Public function addressbook()
    {
        $this -> db -> select('emailid');
        $this -> db ->where('emailid <>','');
        $result = $this -> db -> get('addressbook');
        if ($result -> num_rows() > 0)
        {
            foreach ($result->result() as $row)
            {
                $data[] = $row -> emailid;
            }
            return ($data);
        }
        else
        {
            return FALSE;
        }
    }

1 个答案:

答案 0 :(得分:2)

您未在控制器中的任何位置传递术语

当然,使用该术语来查询数据库,例如:

$this->db->like('column_name', $term);  

对于您的代码,可能是:

Public function getaddressbook() {
        $this -> load -> model('common_model');
        $term = $this->input->get('term');
        $data = $this->common_model->addressbook($term);
        echo json_encode($data);
    }

Public function addressbook($term)
    {
        $this -> db -> select('emailid');
        $this -> db ->where('emailid <>','');
        $this->db->like('column_name', $term);
        $result = $this -> db -> get('addressbook');
        if ($result -> num_rows() > 0)
        {
            foreach ($result->result() as $row)
            {
                $data[] = $row -> emailid;
            }
            return ($data);
        }
        else
        {
            return FALSE;
        }
    }