繁体   English   中英

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

[英]Jquery UI autocomplete plugin not working

我是一个新学习者,试图使用JQuery自动完成插件。 但无法在自动完成建议中获得结果,虽然我在console.log获得结果并作为alert 但不在建议清单中。

输入字段

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

调节器

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

}

在控制台日志输出: 在此处输入图像描述

并且在警报中我得到了object-object但是当我使用JSON.stringify输出时是一个数组

您是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