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

我使用下面的代码来呈现我的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); }; }); 

不知道为什么它抛出这个错误,或者我必须做些什么来克服它…任何帮助表示赞赏。

由于我刚刚join,无法评论drcforbin的post,我想我必须添加自己的答案。

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

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

以下是Custom Data demo中的jQuery UI 1.10.0的_renderItem函数需要改变的地方: http : //jqueryui.com/autocomplete/#custom-data

原始码:

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

请注意自动完成item.autocomplete的更改。 我已经证实,这在我自己的项目中工作。

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

我使用的是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 ); }; 

我知道我迟到了,但如果将来的人还没有得到答案

  .data(“ui-autocomplete-item”,item)

去工作然后尝试这个insted

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

它为我工作,我有loginfunktion问题..我无法login,因为它说

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

希望这有助于某人:)

/卡欣

而现在,使用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的版本,可以是“autocomplete”或“ui-autocomplete”,我把这个更新的combobox插入来解决这个问题(〜78-85)

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