gpt4 book ai didi

javascript - jQuery UI 自动完成通过 ajax 错误 :

转载 作者:行者123 更新时间:2023-11-29 09:53:00 25 4
gpt4 key购买 nike

我已经搜索了几天,但找不到解决方法。

这是我的代码(简化为核心功能):

$("input").autocomplete({
source: function( request, response ){
$.ajax({
url: 'inc/ajax.php',
type: "GET",
async: true,
dataType: "json",
data: {
'task' : 'tasktodo',
'squery' : request.term
},
success:
function( data ) {
response($.map( data, function(item){
return {
label : item['name'],
value : item['name']
}
}));
}
});
}
});

自动完成确实有效,但我在浏览器的控制台中收到以下错误:

 Uncaught TypeError: Object  has no method 'results' (in Chrome)
TypeError: this.options.messages.results is not a function (in Firefox)

错误指向 jqueryui.js 中的一行,它在我的脚本中由 "response()" 调用。

即使错误不影响功能,我也想知道为什么会出现错误。

最佳答案

非常古老的问题,但今天仍然很重要,因为它发生在我身上,我不确定接受的答案涵盖所有基础,或解释问题。

发生这种情况是因为自动完成插件希望您提供一个带有 noResults 和 results 属性的消息对象,以告诉它如何标记搜索结果。

noResults 属性应该是一个字符串,当您猜对了,没有结果时显示。

results 属性应该是一个接受计数参数并返回字符串的方法。

像这样:

$("input").autocomplete({
source: function( request, response ){
... your $.ajax request stuff
},
messages: {
noResults: "No results",
results: function(count){
return count + (count == 0 ? ' result' : ' results');
}
}
});

调用 response(data); 可能根本不需要设置这些属性。但就我而言,一位同事提供了一个消息对象,但将 noResults 和 results 属性都设置为字符串值,因此出现错误:

this.options.messages.results is not a function

关于javascript - jQuery UI 自动完成通过 ajax 错误 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440696/

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