最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I add jquery ui autocomplete to a dynamically created element in jQuery 1.10? - Stack Overflow

programmeradmin1浏览0评论

This is nearly a verbatim post of this question. However, once you flip over to jQuery 1.10, the code no longer works.

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 / ?

$(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.autoplete", "input.searchInput",function(){
    $(this).autoplete(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. 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.

REFERENCE: For 1.10 solution see fiddle in ments of chosen answer, otherwise using 1.9, see fiddle in chosen answer directly.

This is nearly a verbatim post of this question. However, once you flip over to jQuery 1.10, the code no longer works.

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/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.autoplete", "input.searchInput",function(){
    $(this).autoplete(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. 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.

REFERENCE: For 1.10 solution see fiddle in ments of chosen answer, otherwise using 1.9, see fiddle in chosen answer directly.

Share Improve this question edited May 23, 2017 at 11:49 CommunityBot 11 silver badge asked Jan 3, 2014 at 16:49 sacredfaithsacredfaith 8701 gold badge8 silver badges23 bronze badges 2
  • 3 you are not using delegation here, syntax is slightly different, check doc e.g equivalent to live() is: $(document).on("focus","input.searchInput", function() {...}); – A. Wolff Commented Jan 3, 2014 at 16:54
  • @sacredfaith: You can do this all in one line, without delegation. Please see my answer below. – Alex Shilman Commented Jan 3, 2014 at 17:43
Add a ment  | 

4 Answers 4

Reset to default 5

You didn't included jquery ui library and also use it like this for dynamic elements

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

FIDDLE

autoplete 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. So you can drop that whole block of code, and just call autoplete in your addInput function.

I've updated your fiddle (also added jqueryUI JS and CSS)

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

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().autoplete(options).focus();
  };

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

$("input#addButton").click(addInput);
});
 $(document).on('focus', "input.searchInput", function() {
    $(this).autoplete(options);
});

http://jsfiddle/6t74T/459/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论