无法设置属性'_renderItem'未定义的jQuery UI使用HTML自动完成

Cannot set property '_renderItem' of undefined jQuery UI autocomplete with HTML

本文关键字:HTML 使用 UI jQuery 未定义 属性 设置 renderItem      更新时间:2023-09-26

我正在使用以下代码将jQuery UI自动完成项呈现为HTML。项目在自动完成控件中正确呈现,但我一直收到这个javascript错误,无法越过它。

Firefox无法转换JavaScript参数

Chrome无法设置未定义的的属性"_renderItem"

  donor.GetFriends(function (response) {
    // setup message to friends search autocomplete
    all_friends = [];
    if (response) {
        for (var i = 0; i < response.all.length - 1; i++) {                
                all_friends.push({
                    "label":"<img style='padding-top: 5px; width: 46px; height: 46px;' src='/uploads/profile-pictures/" +
                        response.all[i].image + "'/><br/><strong style='margin-left: 55px; margin-top: -40px; float:left;'>" +
                        response.all[i].firstname + " " + response.all[i].lastname + "</strong>",
                    "value":response.all[i].firstname + " " + response.all[i].lastname,
                    "id":response.all[i].user_id});
            }
        }        
    $('#msg-to').autocomplete({
        source:all_friends,
        select:function (event, ui) {               
            // set the id of the user to send a message to
            mail_message_to_id = ui.item.id;
        }
    }).data("autocomplete")._renderItem = function (ul, item) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").html(item.label))
            .appendTo(ul);
    };
});

不知道它为什么会出现这个错误,也不知道我必须做些什么才能克服它……任何帮助都将不胜感激。

由于我刚刚加入,无法对drcforbin的上述帖子发表评论,我想我必须添加自己的答案。

drcforbin是正确的,尽管这确实是一个与OP不同的问题。由于刚刚发布的jQueryUI的新版本,现在进入这个线程的任何人都可能面临这个问题。与自动完成相关的某些命名约定在jQuery UI v1.9中已被弃用,在v1.10中已被完全删除(请参阅http://jqueryui.com/upgrade-guide/1.10/#autocomplete)。

然而,令人困惑的是,他们只提到了从项.autocomplete数据标记到ui autocomplete项的转换,但autocomplete据标记也被重命名为ui autocomplete。更令人困惑的是,演示仍然使用旧的语法(因此被破坏了)。

以下是自定义数据演示中jQuery UI 1.10.0的_renderItem函数需要更改的内容:http://jqueryui.com/autocomplete/#custom-数据

原始代码:

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

固定代码:

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

请注意自动完成项的更改。自动完成我已经验证了这在我自己的项目中有效。

我遇到了同样的问题。。。似乎在以后的版本中,它必须是.data("ui-autocomplete")而不是.data("autocomplete")

我知道我的答案迟到了,但如果未来的人仍然没有得到

.data("ui自动完成项",项)

工作,然后尝试这个指示的

$(document).ready(function(){
 $('#search-id').autocomplete({
  source:"search.php",
  minLength:1,       
  create: function () {
   $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
      return $('<li>')
        .append( "<a>" + item.value + ' | ' + item.label + "</a>" )
        .appendTo(ul);
    };
  }
 })
});

它对我有效,我在登录恐惧方面遇到了问题。。我无法登录,因为上面写着

Uncaught TypeError:无法设置未定义的属性"_renderItem"

希望这对某人有所帮助:)

/kahin

我使用的是jquery 1.10.2,它使用的是:

.data( "custom-catcomplete" )._renderItem = function( ul, item ) {
  return $( "<li>" )
    .data( "ui-autocomplete-item", item )
    .append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
    .appendTo( ul );
};

现在,使用jQuery-2.0.0,它是新模块的名称,但将"."(点)替换为"-"(短划线):

jQuery.widget ('custom.catcomplete', jQuery.ui.autocomplete, {
    '_renderMenu': function (ul, items) {
        // some work here
    }
});
$this.catcomplete({
    // options
}).data('custom-catcomplete')._renderItem = function (ul, item) {}

为了任何偶然发现这篇帖子的人而发帖。

如果不将.autocomplete放在文档就绪事件中,则此错误也会显现出来。

以下代码将失败:

<script type="text/javascript">
    $('#msg-to').autocomplete({
        source:all_friends,
        select:function (event, ui) {               
            // set the id of the user to send a message to
            mail_message_to_id = ui.item.id;
        }
    }).data("autocomplete")._renderItem = function (ul, item) {
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append($("<a></a>").html(item.label))
            .appendTo(ul);
    };
</script>

而下面的代码将起作用:

<script type="text/javascript">
    $(function(){
        $('#msg-to').autocomplete({
            source:all_friends,
            select:function (event, ui) {               
                // set the id of the user to send a message to
                mail_message_to_id = ui.item.id;
            }
        }).data("autocomplete")._renderItem = function (ul, item) {
            return $("<li></li>")
                .data("item.autocomplete", item)
                .append($("<a></a>").html(item.label))
                .appendTo(ul);
        };
    });
</script>

根据您使用的jquery ui的版本,它将是"自动完成"或"ui自动完成",我对组合框插件进行了更新以修复问题(~ln 78-85)

var autoComplete = input.data("ui-autocomplete");
if(typeof(autoComplete) == "undefined")
     autoComplete = input.data("autocomplete");
autoComplete._renderItem = function(ul, item) {