/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>javascript - Twitter bootstrap typeahead unable to add to dynamically created element - Stack Overflow|Concepts Of Algorithm

javascript - Twitter bootstrap typeahead unable to add to dynamically created element - Stack Overflow

admin2025-04-19  2

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 page load.

$("#job_title").typeahead({
    source: searchFunction,        
    onselect: function(obj) {            
    }
});

But if I add the text input with id="job_title" dynamically, the above function doesn't work. Is there a workaround for this?

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 page load.

$("#job_title").typeahead({
    source: searchFunction,        
    onselect: function(obj) {            
    }
});

But if I add the text input with id="job_title" dynamically, the above function doesn't work. Is there a workaround for this?

Share edited Aug 10, 2019 at 16:53 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 14, 2012 at 16:23 Adam LevittAdam Levitt 10.5k27 gold badges89 silver badges145 bronze badges 14
  • 2 Fix the missing quote before someone thinks that's the problem – Esailija Commented Jun 14, 2012 at 16:25
  • Oops, yeah that was just a bad paste error. Thanks for pointing that out. but this is not the issue :) – Adam Levitt Commented Jun 14, 2012 at 16:26
  • Is it a problem to initialize the typeahead on the element when you add it? – Esailija Commented Jun 14, 2012 at 16:28
  • Nope, this code works perfectly if the #job_title element is already on the page inside the HTML. This code only does not work if I dynamically add the #job_title element. – Adam Levitt Commented Jun 14, 2012 at 16:29
  • 1 You need to add the code how you create the element and consequently call .typeahead on it. Because jsfiddle/9DxLZ/1 is working just fine. – Esailija Commented Jun 14, 2012 at 16:36
 |  Show 9 more ments

3 Answers 3

Reset to default 3

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

$("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
    $(this).find(".typeahead").typeahead();
});

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

$("#job_title").livequery("create", function() {
    $(this).typeahead({
        source: searchFunction,        
        onselect: function(obj) {            
        }
    });
});
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();

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745072794a283386.html

最新回复(0)