Twitter bootstrap typeahead unable to add to dynamically created element

后端 未结 3 1224
时光取名叫无心
时光取名叫无心 2021-01-18 12:46

Is there a way to convert twitter bootstrap\'s typeahead function into a $(\"body\").on() function? The following code works fine if the element exists on the p

相关标签:
3条回答
  • 2021-01-18 13:24

    I downloaded jquery.livequery.js to get the behavior I wanted:

    $("#job_title").livequery("create", function() {
        $(this).typeahead({
            source: searchFunction,        
            onselect: function(obj) {            
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-18 13:27

    This solution requires no additional javascript. From https://stackoverflow.com/a/15094730

    $("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
        $(this).find(".typeahead").typeahead();
    });
    
    0 讨论(0)
  • 2021-01-18 13:38
    var fx = {
        "init":function() {
            $("#id_of_an_element").typeahead({
                minLength:2,
                delay:1 ...
            })
        },
        "method2":function(){
            some content;
        }
    }
    

    Every time the element containing the typeahead is loaded into the DOM (eg. via AJAX success, call fx.init();

    0 讨论(0)
提交回复
热议问题