简体   繁体   English

jQuery UI自动完成未显示建议

[英]jQuery UI AutoComplete Not Showing Suggestions

I am using the following code to dynamically create an INPUT element and assign auto complete features to it. 我正在使用以下代码动态创建INPUT元素并为其分配自动完成功能。 There is some problem with the .autocomplete line because the next line which adds the element to td never gets executed. .autocomplete行存在一些问题,因为将元素添加到td的下一行从不执行。

var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];

var fileCodeAutoComplete = $("<input/>");

$(fileCodeAutoComplete).autocomplete({
    source: availableTags
}); 

$(td).append(fileCodeAutoComplete);

Any ideas? 有任何想法吗?

UPDATE 1: 更新1:

I placed a try-catch around the autocomplete call and found the error to be: 我在自动完成调用周围放置了try-catch,发现错误是:

"object does not support this property or method". “对象不支持此属性或方法”。

This is weird because I have added the jquery-ui reference to the page. 这很奇怪,因为我已将jquery-ui引用添加到该页面。

UPDATE 2: 更新2:

I updated the code to follows and still receive the "object does not support this property or method". 我更新了以下代码,但仍然收到“对象不支持此属性或方法”。

var fileCodeAutoComplete = $("<input/>");
    $(fileCodeAutoComplete).attr("id", "fileCodeAutoComplete"); 

    try {
        $("input#fileCodeAutoComplete").autocomplete({
            source: availableTags
        });
    }
    catch(ex) {
        alert(ex.message); 
    }

    $(td).append(fileCodeAutoComplete);

UPDATE 3: I made a new project and copy pasted the code and it worked but on the existing project it does not seems to be working. 更新3:我创建了一个新项目,然后复制粘贴的代码,它可以正常工作,但是在现有项目上,它似乎无法正常工作。 I think it might be the Microsoft Library is getting in the way. 我认为可能是Microsoft Library正在妨碍您。

UPDATE 4: SOLUTION 更新4:解决方案

The problem was that someone else was referring to an older version of jQuery which was messing with the jQuery UI framework. 问题是其他人指的是旧版本的jQuery,该版本与jQuery UI框架混淆了。

var fileCodeAutoComplete = $("<input/>");

try giving in a selector rahter than a tag, like for example 尝试使用选择器而不是标签,例如

<input id="autocomplete" />

then you give 然后你给

$("input#autocomplete").autocomplete({
source: availableTags
});

you can refer the link 你可以参考链接

have you tried adding attributes to the input tag? 您是否尝试过将属性添加到输入标签? something like: 就像是:

var fileCodeAutoComplete = $("<input type='text' />");

Also, how about trying the autocomplete on a known input element to see if the autocomplete functionality is breaking it, or if it's the dynamic input field you're adding the functionality to? 另外,如何在已知输入元素上尝试自动完成功能以查看自动完成功能是否正在破坏它,或者它是要向其添加功能的动态输入字段?

When you declare: 当您声明:

var fileCodeAutoComplete = $("<input/>");

It's already a jquery object, you don't have to wrap it in $() again. 它已经是一个jquery对象,您不必再次将其包装在$()中。 you can also chain the creation of the input and the autocomplete: 您还可以链接输入和自动完成的创建:

var fileCodeAutoComplete = $("<input/>")
    .autocomplete({
        source: availableTags
    })
    .appendTo(td);

Your solution works for me: 您的解决方案对我有用:

http://jsfiddle.net/L8yvb/ http://jsfiddle.net/L8yvb/

Works for me in JS Fiddle: http://jsfiddle.net/bH9ab/1/ 在JS Fiddle中为我工作: http//jsfiddle.net/bH9ab/1/

I used your original code nearly verbatim (other than mocking a table cell to append the input). 我几乎逐字使用了原始代码(除了模拟表单元格以追加输入外)。

To validate that the reference to jQuery UI is correct (and that it isn't corrupt, truncated, etc. on your local machine), you can point to Google's CDN: 为了验证对jQuery UI的引用是正确的(并且在本地计算机上没有损坏,截断等),您可以指向Google的CDN:

https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM