JqueryUI自动完成错误:未捕获TypeError:对象#的属性“结果”不是函数

时间:2022-11-05 12:16:11

I've looked around and can't find an answer to my problem. I have not used jquery UI much, but I'm trying to implement an autocomplete using this blog entry jQuery UI Autocomplete with JSON in MVC 4 as a guide because it's practically identical to what I need. I'm probably missing something "obvious" because I don't understand all the pieces of the autocomplete syntax yet.

我环顾四周,无法找到问题的答案。我没有多少使用jquery UI,但是我正在尝试使用这个博客条目jQuery UI Autocomplete和MVC 4中的JSON作为指南来实现自动完成,因为它几乎与我需要的相同。我可能遗漏了一些“显而易见”的东西,因为我还不了解自动完成语法的所有部分。

Issue: I can get the dropdown of suggestions to appear. But as soon as it does I get a

问题:我可以得到建议的下拉列表。但是一旦我得到了

 Uncaught TypeError: Property 'results' of object #<Object> is not a
 function

error in the console. Also, though the suggestions appear, I can't select any of them. the list disappears as soon as I try. Though that could be something else altogether.

控制台中出错。此外,虽然建议出现,我不能选择任何一个。一旦我尝试,清单就会消失。虽然这可能完全不同。

The location of the error in the jqueryUI1.9.2 code is the last line in this snippet:

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 );

My jquery looks like this:

我的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: ""
        }
    });

My controller looks like this:

我的控制器看起来像这样:

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);
}

Any idea where I'm going wrong?

知道我哪里错了吗?

2 个解决方案

#1


17  

As you have pointed in a comment to Narendra answer, the problem is with messages option. As answered here, results property, in messages option, must be a function instead of a string

正如您在对Narendra的回答中所指出的那样,问题在于消息选项。如此处所述,messages属性,在messages选项中,必须是函数而不是字符串

jQuery(...).autocomplete({
    messages : {
        noResults : '',
        results : function(resultsCount) {}
    }
});

#2


0  

Seems like you are referring to a variable and trying to access the same as a function.

好像你指的是一个变量并尝试访问它作为一个函数。

__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;

Just change the code assign the content to result and it would work.

只需更改代码将内容分配给结果,它就可以工作。

#1


17  

As you have pointed in a comment to Narendra answer, the problem is with messages option. As answered here, results property, in messages option, must be a function instead of a string

正如您在对Narendra的回答中所指出的那样,问题在于消息选项。如此处所述,messages属性,在messages选项中,必须是函数而不是字符串

jQuery(...).autocomplete({
    messages : {
        noResults : '',
        results : function(resultsCount) {}
    }
});

#2


0  

Seems like you are referring to a variable and trying to access the same as a function.

好像你指的是一个变量并尝试访问它作为一个函数。

__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;

Just change the code assign the content to result and it would work.

只需更改代码将内容分配给结果,它就可以工作。