javascript Jquery 自动完成选择类型错误:ui.item 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18886104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:34:11  来源:igfitidea点击:

Jquery Autocomplete Select TypeError: ui.item undefined

javascriptjqueryhtmljquery-uiautocomplete

提问by Ela Buwa

I am using jquery ui 1.10.3 and jquery 2.0.3. I am trying to use the autocomplete function to change text of another text box on selecting an option from the suggested options from autocomplete.

我正在使用 jquery ui 1.10.3 和 jquery 2.0.3。我正在尝试使用自动完成功能在从自动完成的建议选项中选择一个选项时更改另一个文本框的文本。

Below is my code for the autocomplete function. I do get the results as needed but when I select an option from it, I get the TypeError: ui.item is undefinederror.

下面是我的自动完成功能的代码。我确实根据需要获得了结果,但是当我从中选择一个选项时,我得到了TypeError: ui.item is undefined错误。

<script language="javascript">
$(document).ready(function(){
    $('#item_code').autocomplete({
    source: "http://localhost/test/item/search_item",
        minLength: 1,
        select: function( event, ui ) {
            $( "#item_description" ).val(ui.item.description );
            return false;
        }
    }).data("ui-autocomplete" )._renderItemData = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.value + " - " + item.description + "</a>" )
            .appendTo( ul );
    };
});   
</script>

I have scoured the net but I have come to a point where I find banging my head on the table. Any help is greatly appreciated.

我已经搜索了网,但我发现我的头撞在桌子上。任何帮助是极大的赞赏。

回答by joshschreuder

You should only need to change the one data property:

您应该只需要更改一个数据属性:

.data('item.autocomplete')

was deprecated in favour of

被弃用而赞成

.data('ui-autocomplete-item')

As of jQuery UI 1.9 and removed as of jQuery UI 1.10

从 jQuery UI 1.9 开始,从 jQuery UI 1.10 开始移除

http://jqueryui.com/upgrade-guide/1.10/#removed-item-autocomplete-data-use-ui-autocomplete-item

http://jqueryui.com/upgrade-guide/1.10/#re​​moved-item-autocomplete-data-use-ui-autocomplete-item

回答by gunnerz

I had a similar problem, but this was because the jQuery documentation now shows the usage for jQuery UI 1.10 and our website is still using jQuery UI 1.8.20.

我遇到了类似的问题,但这是因为 jQuery 文档现在显示了 jQuery UI 1.10 的用法,而我们的网站仍在使用 jQuery UI 1.8.20。

This is what worked for me in the end.

这最终对我有用。

  .data("autocomplete")._renderItem = function (ul, item) {
      return $("<li>")
      .data("item.autocomplete", item)
      .append("<a>" + item.label + "<br><b>" + item.category + "</b></a>").appendTo(ul);
  };

回答by Ela Buwa

turns out I had to change

结果我不得不改变

data("ui-autocomplete" )._renderItemData = function( ul, item ) {

and

.data( "item.autocomplete", item )

to

data("ui-autocomplete" )._renderItem = function( ul, item ) {

and

.data( "item.autocomplete-item", item )

hope this helps anyone who has migration issues with jQuery UI

希望这可以帮助任何有 jQuery UI 迁移问题的人