Jquery 自动完成赢得';“我不能和”一起工作;如果;陈述
我在我的项目中使用jQuery Autocomplete,但是我遇到了一些问题 我的自动完成正在工作,但我想在自动完成的最后一个结果中添加一个永久项目。为此,我使用了_renderItem(因为我想为这个项目使用不同的样式,居中,背景色:#000) 工作代码:Jquery 自动完成赢得';“我不能和”一起工作;如果;陈述,jquery,autocomplete,Jquery,Autocomplete,我在我的项目中使用jQuery Autocomplete,但是我遇到了一些问题 我的自动完成正在工作,但我想在自动完成的最后一个结果中添加一个永久项目。为此,我使用了_renderItem(因为我想为这个项目使用不同的样式,居中,背景色:#000) 工作代码: 所以我想知道,我做错了什么?为什么我会收到此错误?对于一般情况,您不会返回任何内容。只有您的特定情况会得到处理,但“正常”项首先出现,它们是一条死胡同,因为之后函数将不返回任何内容 因此,为默认情况添加一个简单的else: .data("
所以我想知道,我做错了什么?为什么我会收到此错误?对于一般情况,您不会返回任何内容。只有您的特定情况会得到处理,但“正常”项首先出现,它们是一条死胡同,因为之后函数将不返回任何内容 因此,为默认情况添加一个简单的
else
:
.data("ui-autocomplete")._renderItem = function (ul, item) {
if (item.type==="noneResult"){
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append($("<a></a>").html(item.label))
.append("<div style='text-align: center;'>" + item.label + "</div>")
.appendTo(ul);
}
else
{
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append($("<a></a>").html(item.label))
.append("<div style='text-align: center;'>" + item.label + "</div>")
.appendTo(ul);
}
}
.data(“ui自动完成”)。\u renderItem=函数(ul,项目){
如果(项目类型==“非结果”){
返回$(“”)
.data(“ui自动完成项”,项)
.append($(“”).html(item.label))
.append(“+item.label+”)
.附录(ul);
}
其他的
{
返回$(“”)
.data(“ui自动完成项”,项)
.append($(“”).html(item.label))
.append(“+item.label+”)
.附录(ul);
}
}
Wooow,真的谢谢你,伙计!我一直在努力解决这个问题。接受我最好的回答!飞行的意大利面怪物祝福你!
.data("ui-autocomplete")._renderItem = function (ul, item) {
if (item.type==="noneResult"){
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append($("<a></a>").html(item.label))
.append("<div style='text-align: center;'>" + item.label + "</div>") .appendTo(ul);
}
}
$.getJSON(
'<%= my_rails_path %>.json',
{search: term},
function(json) {
var result = [];
$.each(json, function(key, value) {
var item = {};
item.type = ' ';
item.label = value.name;
item.value = value.name;
result.push(item);
})
var item = {};
item.type = "noneResult";
item.label = "Create a new book called '" + term + "'.";
item.value = term;
result.push(item)
response(result);
}
);
Uncaught TypeError: Cannot call method 'data' of undefined
.data("ui-autocomplete")._renderItem = function (ul, item) {
if (item.type==="noneResult"){
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append($("<a></a>").html(item.label))
.append("<div style='text-align: center;'>" + item.label + "</div>")
.appendTo(ul);
}
else
{
return $("<li></li>")
.data("ui-autocomplete-item", item)
.append($("<a></a>").html(item.label))
.append("<div style='text-align: center;'>" + item.label + "</div>")
.appendTo(ul);
}
}