gpt4 book ai didi

JQuery UI 自动完成渲染类别与链接

转载 作者:行者123 更新时间:2023-12-03 22:51:29 25 4
gpt4 key购买 nike

我正在使用 JQuery UI 自动完成功能来快速搜索小部件。我有几个分组项目,如酒店、城市、地区等。我可以呈现类别,但无法链接它们。当我尝试时,ui 自动完成插件会检测项目等类别。这不是问题,但是当我使用向上/向下箭头或使用鼠标将它们聚焦时,它会返回如下错误:

“类型错误:项目未定义。this.liveRegion.text( item.value );”

我该如何解决这个问题?我尝试将 JQuery UI Autocomplete 的“focus”事件与多种方法一起使用(例如“return false”、e.stopPropagatione.preventDefault') ,但没有成功

这是我的代码:

$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function (ul, items) {
var searchkey = "";
var itemtype = "";
var searchtype = "";

var self = this, currentCategory = "";
$.each(items, function (index, item) {
if (typeof item.kelime != 'undefined') { searchkey = item.kelime; }
if (item.category != currentCategory) {
if (item.category == "Bölge" || item.category == "Şehir") {
itemtype = "cat-bolgeler";
} else if (item.category == "Otel") {
itemtype = "cat-oteller";
searchtype = "otel";
} else if (item.category == "Yurt Dışı Tur") {
itemtype = "cat-ydtur";
searchtype = "yurtdisitur";
} else if (item.category == "Yurt İçi Tur") {
itemtype = "cat-yitur";
searchtype = "yurticitur";
} else if (item.category == "Cruise") {
itemtype = "cat-cruise";
searchtype = "cruise";
}
if (searchtype != "") {
ul.append("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'><a href='/arama/" + searchkey + "?k=" + searchtype + "&q=" + searchkey + "'>" + item.category + "</a></li>");
} else {
ul.append("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'>" + item.category + "</li>");
}
currentCategory = item.category;
}
self._renderItem(ul, item);
});
}
});

$(".hizliaratext").catcomplete({
source: function (request, response) {
$.ajax({
url: '/filename.aspx',
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "get",
data: { kelime: request.term },
success: function (data) {
response($.map(data, function (item) {
return {
label: item.label,
searchid: item.searchid,
category: item.category,
link: item.link,
kelime: item.kelime
}
}));
}
});
},
minLength: 3,
appendTo: "#hizliara",
select: function (event, ui) {
window.location = ui.item.link;
},
focus: function (event, ui) {
}
}).data("catcomplete")._renderItem = function (ul, item) {
return $("<li>").data("item.autocomplete", item).append("<a>" + item.label + "</a>").appendTo(ul);
};

以下是“?kelime=anka”的示例 JSON 响应:

[{"searchid":2001,"label":"Alba Ankara Hotel","category":"Otel","link":"/otel-detay/alba-ankara-hotel","kelime":"anka"},{"searchid":2238,"label":"Ankara Madi İnci Hotel","category":"Otel","link":"/otel-detay/ankara-madi-inci-hotel"},{"searchid":2244,"label":"Madi Hotel Ankara","category":"Otel","link":"/otel-detay/madi-hotel-ankara"},{"searchid":2403,"label":"City Hotel Ankara","category":"Otel","link":"/otel-detay/city-hotel-ankara"},{"searchid":2404,"label":"Doğa Residence Ankara","category":"Otel","link":"/otel-detay/doga-residence-ankara"},{"searchid":6779,"label":"Ankara","category":"Şehir","link":"/ustaramaliste.aspx?y=6779"},{"searchid":6785,"label":"Ankara / Çankaya","category":"Bölge","link":"/ustaramaliste.aspx?y=6785"},{"searchid":14601,"label":"İzmir / Çankaya","category":"Bölge","link":"/ustaramaliste.aspx?y=14601"}]

最佳答案

听起来(和看起来)就像您想要类别项目的select/focus事件(如果不是这种情况,我将更新我的答案)。

自动完成小部件内部期望列表项具有与其关联的 item.autocomplete 数据。为了解决该错误,您可以使用适当的数据创建“类别”项。这将使您能够对 select 事件使用react,并消除 focus 事件上发生的错误:

更新的小部件代码:

$.widget("custom.catcomplete", $.ui.autocomplete, {
_renderMenu: function(ul, items) {
var searchkey = "";
var itemtype = "";
var searchtype = "";

var self = this,
currentCategory = "";
$.each(items, function(index, item) {
if (typeof item.kelime != 'undefined') {
searchkey = item.kelime;
}
if (item.category != currentCategory) {
if (item.category == "Bölge" || item.category == "Şehir") {
itemtype = "cat-bolgeler";
} else if (item.category == "Otel") {
itemtype = "cat-oteller";
searchtype = "otel";
} else if (item.category == "Yurt Dışı Tur") {
itemtype = "cat-ydtur";
searchtype = "yurtdisitur";
} else if (item.category == "Yurt İçi Tur") {
itemtype = "cat-yitur";
searchtype = "yurticitur";
} else if (item.category == "Cruise") {
itemtype = "cat-cruise";
searchtype = "cruise";
}
if (searchtype != "") {
ul.append($("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'><a href='/arama/" + searchkey + "?k=" + searchtype + "&q=" + searchkey + "'>" + item.category + "</a></li>").data("item.autocomplete", {}));
} else {
ul.append($("<li class='ui-autocomplete-category " + itemtype + "' id='" + item.searchid + "'>" + item.category + "</li>").data("item.autocomplete", {}));
}
currentCategory = item.category;
}
self._renderItem(ul, item);
});
}
});

示例: http://jsfiddle.net/J5rVP/20/

关于JQuery UI 自动完成渲染类别与链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com