我正在为不同的领域使用 JQuery UI 自动完成功能。为了获取数据,我使用一个函数作为源。它工作得很好!
我想知道是否有一种方法可以不使用源代码中的匿名函数,而是声明一个通用函数,该函数将具有重定向到正确 URL 的参数。
我是 JS 和 JQuery 的新手,所以我不知道匿名函数中的参数请求和响应来自什么。
这是我想要做的:
$ac.autocomplete({
//Call the function here, but what are the parameter request and response???
source: autocomplete(),
minLength: 1
});
这是我想调用的函数
function autoComplete(request, response, url) {
$.ajax({
url: '/Comp/'+url,
dataType: "json",
type: "POST",
success: function (data) {
response($.map(data, function(item) {
return { label: item, value: item, id: item };
}));
}
});
}
非常感谢你的帮助。
最佳答案
你应该使用
source: autoComplete
代替
source: autocomplete()
再说一句。 jQuery UI 自动完成的默认实现仅使用
value
和 label
并且不使用 id
.
关于jquery-ui - JQuery 自动完成源是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153763/