简体   繁体   English

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

[英]Jquery UI autocomplete plugin not working

I am a new learner, trying to use JQuery autocomplete plugin. 我是一个新学习者,试图使用JQuery自动完成插件。 but unable to get the results in autocomplete suggestion, although I am getting the results in console.log and as an alert . 但无法在自动完成建议中获得结果,虽然我在console.log获得结果并作为alert But not in suggestion list. 但不在建议清单中。

Input field 输入字段

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

jquery 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 并且在警报中我得到了object-object但是当我使用JSON.stringify输出时是一个数组

Seams that you're ajax response format issue use map function to format ajax response - 您是ajax响应格式问题的接缝使用map函数来格式化ajax响应 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM