duanpin2034 2019-05-07 05:44
浏览 69

Jquery UI自动完成插件无法正常工作

I am a new learner, trying to use JQuery autocomplete plugin. but unable to get the results in autocomplete suggestion, although I am getting the results in console.log and as an alert. But not in suggestion list.

Input field

'Name: <input type="text" id="hint" name="hint" />'

jquery

$(document).ready(function () {
    $("#hint").keyup(function () {
            $( "#hint" ).autocomplete({
        source: function(request, response) {
            //console.info(request, 'request');
            //console.info(response, 'response');

            $.ajax({
                //q: request.term,
                url: "<?php echo base_url(); ?>index.php?Librarian/autocomplete/",
                data: { term: $("#hint").val()},
                dataType: "json",
                type: "POST",
                success: function(data) {
                    alert(data)
                    console.log(data);
                    response(data);
                }
            });
        },
        minLength: 2
    });
  }); 
});

Controller

function autocomplete($param1 = '', $param2 = '', $param3 = '') {
    // load model
    $this->load->model("Librarian_model");
    $search_data = $this->input->post('term');


    $result = $this->Librarian_model->get_autocomplete($search_data);

    echo json_encode($result); 
    //returning the result as result_array() also tried implode function but got the error at response

}

OUtput at console log : enter image description here

and in alert i got object-object but when I use JSON.stringify output is a array

  • 写回答

1条回答 默认 最新

  • dongou0524 2019-05-07 06:16
    关注

    Seams that you're ajax response format issue use map function to format ajax response -

    $(document).ready(function () {
        $("#hint").keyup(function () {
                $( "#hint" ).autocomplete({
            source: function(request, response) {
                //console.info(request, 'request');
                //console.info(response, 'response');
    
                $.ajax({
                    //q: request.term,
                    url: "<?php echo base_url(); ?>index.php?Librarian/autocomplete/",
                    data: { term: $("#hint").val()},
                    dataType: "json",
                    type: "POST",
                    success: function(data) {
                        alert(data)
                        console.log(data);
    
    
                        response($.map(data, function (item) {
                            return {
                                label: item.name,
                                value: item.name
                            };
                        }));
    
    
                    }
                });
            },
            minLength: 2
        });
      }); 
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私