简体   繁体   English

在DOM上动态创建的元素上触发事件,这可能吗?

[英]Trigger event on elements created on the fly at DOM, it's possible?

I'm using jQuery Autocomplete library and apply the behavior to any input with class typeahead . 我正在使用jQuery Autocomplete库,并将该行为应用于typeahead类的任何输入。 That's works for any element that is on the DOM when page loads. 这适用于页面加载时DOM上的任何元素。 I create some elements on the fly by adding some HTML to the DOM using append or clone or any other jQuery/Javascript function for this purpose and all of them also have the class typeahead but behavior isn't triggered on this new elements. 为此,我通过使用appendclone或任何其他jQuery / Javascript函数将一些HTML添加到DOM中来动态创建一些元素,所有这些元素也都typeahead了类,但是在此新元素上未触发行为。 how I make this work? 我该如何工作? I need to trigger the behavior on this new elements too, how? 我也需要在这个新元素上触发行为,怎么办? Any advice? 有什么建议吗?

This is the code for trigger the autocomplete event: 这是触发autocomplete事件的代码:

$('#products_forms .typeahead').autocomplete({
    serviceUrl: Routing.generate('get_products'),
    paramName: 'filter',
    showNoSuggestionNotice: true,
    noSuggestionNotice: 'Nothing found',
    minChars: 3,
    onSelect: function(suggestion) {
        alert('You selected: ' + suggestion.value + ', ' + suggestion.data);
    }
});

Call the typeahead function after the items are added, ex: 添加项目后调用typeahead函数,例如:

var elem = $("<div id='new'></div>");
$("#someContainer").append(elem);
$("#new").autocomplete({});

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

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