无法设置属性'_renderItem'未定义的jQuery UI使用HTML自动完成
Cannot set property '_renderItem' of undefined jQuery UI autocomplete with HTML
我正在使用以下代码将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) {
- index.html使用webpack开发服务器未重新加载
- HTML-使用mysql.php生成一个选择框.我怎样才能更改“;值“;生成的选择框选项的
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小
- 点击浏览器的“后退”按钮显示的是JSON而不是HTML(使用Rails和d3.js)
- HTML-使用CSS和Javascript创建身份证
- javascript html使用webstorage保存表单
- HTML使用选择表单更改输入选择(可能使用JQuery?)
- HTML 使用 JavaScript 添加具有 JQuery 属性的元素
- HTML 使用 Steam API 获取价格
- 对象未定义/HTML使用jQuery输出
- HTML - 使用 Javascript/canvas 和 HTML 将上传的图像拉入 PHP
- html- 使用下拉框搜索
- HTML 5 使用 javascript 切换 contenteditable 属性
- 对象不打印/返回信息到 HTML(使用 jquery .hover())
- HTML - 使用停止PHP脚本的javascript进行输入检查
- 如何将字符串参数从 HTML(使用 JavaScript)传递到 Java 可执行文件 Jar
- JSON到HTML使用JSON.parse未定义错误
- html使用javascript为输入文本赋值是不起作用的
- 如何让空格键{{if}}中的HTML使用Session变量进行反应性更改
- 如何在java中获取完全加载页面的html(使用javascript)作为输入