简体   繁体   English

如何将jQuery ui自动完成功能添加到jQuery 1.10中动态创建的元素?

[英]How can I add jquery ui autocomplete to a dynamically created element in jQuery 1.10?

This is nearly a verbatim post of this question. 这几乎是问题的逐字记录。 However, once you flip over to jQuery 1.10, the code no longer works. 但是,一旦切换到jQuery 1.10,该代码将不再起作用。

I did change the call to the "live" method, and switched it over to utilize the newer "on" method. 我确实将调用更改为“实时”方法,并将其切换为使用较新的“打开”方法。 What else do I need to do to fix this http://jsfiddle.net/sacredfaith/6t74T/458/ ? 我还需要做什么来解决此http://jsfiddle.net/sacredfaith/6t74T/458/

$(function() {
var options = {
    source: [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
        ],
    minLength: 2
};

$(document.body).on("keydown.autocomplete", "input.searchInput",function(){
    $(this).autocomplete(options);
});

var addInput = function() {
    var inputHTML = " <input name='search' value='' class='searchInput' maxlength='20' />";
    $(inputHTML).appendTo("form#myForm");        
    $("input.searchInput:last").focus();
};

if (!$("form#myForm").find("input.searchInput").length) {
    addInput();
}

$("input#addButton").click(addInput);
});

My searches thus far have yielded methods which use deprecated jquery libraries. 到目前为止,我的搜索已经产生了使用不推荐使用的jquery库的方法。 I'm needing a more updated version, but haven't been able to find success for the past several hours of working on this... 我需要一个更新的版本,但是在过去的几个小时中一直没有找到成功的方法...

EDIT: Fiddle link and code updated to reflect "on" syntax changes. 编辑:小提琴链接和代码已更新,以反映“在”语法上的更改。
EDIT2: Thanks to all for your patience. EDIT2:感谢大家的耐心配合。

REFERENCE: For 1.10 solution see fiddle in comments of chosen answer, otherwise using 1.9, see fiddle in chosen answer directly. 参考:对于1.10解决方案,请参阅所选答案的注释,否则请使用1.9,请直接查看所选答案的注释。

You didn't included jquery ui library and also use it like this for dynamic elements 您没有包括jquery ui库,还像这样将其用于动态元素

$(document.body).on('focus', 'input.searchInput' ,function(){
    $(this).autocomplete(options);
});

FIDDLE 小提琴

You can do it all in one line. 您可以一行完成全部操作。 Working DEMO 工作演示

 var addInput = function() {
    $("<input name='search' value='' class='searchInput' maxlength='20' />").appendTo("form#myForm").end().autocomplete(options).focus();
  };

if (!$("form#myForm").find("input.searchInput").length) {
    addInput();
}

$("input#addButton").click(addInput);
});

autocomplete creates a new widget, attached to the given element, so you only need to call it once per input, and not each time the key is pressed. autocomplete会创建一个附加到给定元素的新窗口小部件,因此您只需要为每个输入调用一次,而不是每次按键都调用一次。 So you can drop that whole block of code, and just call autocomplete in your addInput function. 因此,您可以删除整个代码块,只需在addInput函数中调用autocomplete功能即可。

I've updated your fiddle (also added jqueryUI JS and CSS) 我已经更新了您的小提琴 (还添加了jqueryUI JS和CSS)

var addInput = function() {
    var inputHTML = " <input name='search' value='' class='searchInput' maxlength='20' />";
    $(inputHTML).appendTo("form#myForm");        
    $("input.searchInput:last").autocomplete(options).focus();
};
 $(document).on('focus', "input.searchInput", function() {
    $(this).autocomplete(options);
});

http://jsfiddle.net/6t74T/459/ http://jsfiddle.net/6t74T/459/

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

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