我环顾四周,无法找到问题的答案.我没有多少使用
jquery UI,但我正在尝试使用此博客条目
jQuery UI Autocomplete with JSON in MVC 4作为指南实现自动完成,因为它几乎与我需要的相同.我可能遗漏了一些“显而易见”的东西,因为我还不了解自动完成语法的所有部分.
问题:我可以得到建议的下拉列表.但是一旦我得到了
Uncaught TypeError: Property 'results' of object #<Object> is not a function
控制台中出错.
此外,虽然建议出现,我不能选择任何一个.一旦我尝试,清单就会消失.虽然这可能完全不同.
jqueryUI1.9.2代码中的错误位置是此代码段中的最后一行:
__response: function( content ) {
var message;
this._superApply( arguments );
if ( this.options.disabled || this.cancelSearch ) {
return;
}
if ( content && content.length ) {
message = this.options.messages.results( content.length );
我的jquery看起来像这样:
$("#FastCategory").autocomplete({
source: function (request,response) {
$.ajax({
url: "/Quiz/GetCategory",type: "POST",dataType: "json",data: { term: request.term },success: function (data) {
console.log("data=",data);
response($.map(data,function (item) {
console.log("item=",item,item.Description);
return { label: item.Description,value: item.Description };
}))
}
})
},messages: {
noresults: "",results: ""
}
});
我的控制器看起来像这样:
public JsonResult GetCategory(string term)
{
var result = (from r in db.QuizCategories
where r.Description.ToLower().Contains(term.ToLower())
select new { r.Description }).distinct();
return Json(result,JsonRequestBehavior.AllowGet);
}
知道我哪里错了吗?
解决方法
正如您在对Narendra的回答中所指出的那样,问题在于消息选项.如回答
here,结果属性,在消息选项中,必须是函数而不是字符串
jQuery(...).autocomplete({
messages : {
noresults : '',results : function(resultsCount) {}
}
});