我使用此函数将 jquery ui 自动完成函数的结果包装在超链接中。它无法做到这一点。它只显示标签项。这里有什么建议吗?
[{"value":"morzine","label":"Morzine"},{"value":"grachen","label":"Grachen"},{"value":"fiesch","label":"Fiesch"},{"value":"zermatt","label":"Zermatt"},{"value":"saas-fee","label":"Saas Fee"},{"value":"bettmeralp","label":"Bettmeralp"},{"value":"riederalp","label":"Riederalp"},{"value":"jeizinen","label":"Jeizinen"},{"value":"crans-montana","label":"Crans Montana"},{"value":"simplon-dorf","label":"Simplon Dorf"}]
$("#srch").autocomplete({
minLength: 3,
source: function (a, b) {
$.getJSON("http://skiweather.eu/v3/ajax/search.php?q=" + a.term + "&format=json", function (a) {
b(a)
})
},
select: function (a, b) {
$("#srch").val(b.item.label);
return false
},
focus: function (a, b) {
$("#srch").val(b.item.label);
return false
}
}).data("autocomplete")._renderItem = function (a, b) {
return $("<li></li>")
.data("item.autocomplete", b)
.append('<a href="/webcams/' + b.value + '"> ' + b.label + '</a> ')
.appendTo(a)
}
最佳答案
如果您使用的是 jQueryUI >= 1.10,则需要使用 .data('ui-autocomplete')
。如果您查看网站的控制台,您将看到以下错误:
Uncaught TypeError: Cannot set property '_renderItem' of undefined
$("#srch").autocomplete({
minLength: 3,
source: /* ... */,
}).data("ui-autocomplete")._renderItem = function (a, b) {
return $("<li></li>")
.data("item.autocomplete", b)
.append('<a href="/webcams/' + b.value + '"> ' + b.label + '</a> ')
.appendTo(a);
};
示例: http://jsfiddle.net/Aa5nK/51/
另外,不相关,但我建议使用更容易理解的变量名称(比 a
和 b
更具描述性的名称)。
关于jquery - 将 jquery ui 自动完成结果包装在 href 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135063/