douti8321 2013-01-22 19:54 采纳率: 0%
浏览 39
已采纳

用json jquery多次自动完成

I am working on jquery multiple autocomplete, however, while i type something, all the items are published instead of the matching ones. My Javascript is

$('.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;
    }
});

The js/getaddressbook.php returns

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

Not sure where am i going wrong.

Below is the snapshot how it looks like

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

Please help!

My JS controller has the function

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

And the common_model has the function

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条回答 默认 最新

  • douweida2878 2013-01-22 20:15
    关注

    You are not passing the term anywhere in your controller.

    And of course using that term to query your database for example:

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

    For your code, it could be:

    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;
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line