自动完成和 ajax
我使用 $.getJSON
加载字符串数组,将其用作 jquery ui 自动完成中的值。我无法让它工作。
代码(成功函数被正确调用,我看到警报):
$(function () {
$.getJSON(baseUri + 'truck/models/', {}, function (data) {
$("#ModelName").autocomplete({
source: function( request, response ) {
alert(data);
response(data);
}
});
});
});
服务器返回的内容:
["KIRUNA K350","MAFI","SISU TR180","SISU TRX242","SVETRUCK 32T","VOLVO A25D","VOLVO A25E","VOLVO A40","VOLVO BMl120","VOLVO BML90"]
在输入框中键入时收到的错误:
未捕获类型错误:无法读取未定义的属性“元素”
I load a string array using $.getJSON
to use it as values in a jquery ui autocomplete. I can't get it to work.
Code (the success function is being called properly and I see the alert):
$(function () {
$.getJSON(baseUri + 'truck/models/', {}, function (data) {
$("#ModelName").autocomplete({
source: function( request, response ) {
alert(data);
response(data);
}
});
});
});
Content returned by the server:
["KIRUNA K350","MAFI","SISU TR180","SISU TRX242","SVETRUCK 32T","VOLVO A25D","VOLVO A25E","VOLVO A40","VOLVO BMl120","VOLVO BML90"]
Error that I get when I type in the input box:
Uncaught TypeError: Cannot read property 'element' of undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您只是在页面加载时为自动完成选项执行 ajax 获取,因此无需对源使用函数,您只需执行以下操作即可:
It looks like you are just doing the ajax fetch for the autocomplete options on page load so no need to use a function for the source you can just do:
您必须确保设置对象值
因此
({source: data});
例如参见小提琴: http://jsfiddle.net/dDCEW/
You must ensure you set the object value
Thus
({source: data});
see fiddle for example: http://jsfiddle.net/dDCEW/
我有另一个插件,它在 jQuery 上下文中定义了
menu
。我删除了该插件,一切正常。有点恶心吧?I had another plugin which defined
menu
in the jQuery context. I removed that plugin and everything works fine know. Kind of nasty, huh?