简体   繁体   English

自动完成的JQuery-UI未定义列表

[英]Autocomplete JQuery-UI Undefined List

I've been working with jQuery UI Autocomplete to make an suggestion from database and autocomplete the rest of field. 我一直在使用jQuery UI Autocomplete从数据库提出建议并自动完成其余字段。 the javascript working just fine, but it made output like this: javascript可以正常工作,但是输出如下:

But if i press down button and enter it, the field will completed with value from database. 但是,如果我按下按钮并输入,该字段将使用数据库中的值完成。

this is my javascript : 这是我的javascript:

$(function() {
//clear values on refresh
$('#nmbr').val("");

$(".kdbr").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "<?php echo base_url();?>js/coba3.php",
            dataType: "json",
            data: {
                term: request.term
            },
            success: function (data) {
            //  var data = $.parseJSON(response);
                response($.map(data, function (el) {
                    return {
                        kdbr: el.kdbr,
                        nmbr: el.nmbr
                    };
                }));
            }
        });
    },
    select: function (event, ui) {
        // Prevent value from being put in the input:
        event.preventDefault();
        this.value = ui.item.kdbr;
        // Set the next input's value to the "value" of the item.
        $('#nmbr').val(ui.item.nmbr);

    }

});
});

and this is my html code : 这是我的html代码:

<form action="#" method="post">
 <p><label for="kdbr">KDBR</label><br />
     <input type="text" name="kdbr" id="kdbr" class = "kdbr" value="" /></p>
 <p><label for="nmbr">NMBR</label><br />
     <input type="text" name="nmbr" id="nmbr" class = "nmbr" value="" /></p>
</form>

and this is the javascript version that i used 这是我使用的javascript版本

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>

i have to write it to, because when i used version 1.8.1 the list show word 我必须将其写入,因为当我使用1.8.1版时,列表显示单词

undefined 未定义

As I can see on the picture I think you didn't include the styles sheets of jquery-ui because the autocomplete was not styled well. 正如我在图片上看到的,我认为您没有包括jquery-ui的样式表,因为自动完成的样式不正确。

Please include all.css or just the specific autocomplete.css 请包括all.css或仅包括特定的autocomplete.css

https://github.com/jquery/jquery-ui/tree/master/themes/base https://github.com/jquery/jquery-ui/tree/master/themes/base

and with this line 并用这条线

 select: function (event, ui) {
        // Prevent value from being put in the input:
        event.preventDefault();
        this.value = ui.item.kdbr;
        // Set the next input's value to the "value" of the item.
        $('#nmbr').val(ui.item.nmbr);

     return false; //<--- you need to add this

    }

and change the 并更改

 return {
      kdbr: el.kdbr,
      nmbr: el.nmbr
 };

to this 对此

return {
      label: el.kdbr,
       value: el.nmbr
  };

the autocomplete needs a json return value of the items containing that value and its label 自动完成功能需要包含该值及其标签的项目的json返回值

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

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