I have a select2 drop-down for which I provide a matcher function. It is initialised like this on initial page load:
jQuery(document).ready(function() {
jQuery(".my_select2").select2({
matcher: function(term, text) {...}
});
});
That works find on initial page load.
Now, I have additional drop-downs (select
elements created dynamically (pulled in via AJAX, i.e. jQuery(match).load(url)
. These additional drop-downs do not get intialised as select2 widgets, which is understandable, even though they match the original select2 selector.
So, how can I tell jQuery to treat these dynamically-created select
elements as select2 items that need to be initialised? Can I set some kind of "watch" on matching elements so the select2 initialisation kicks in every time a matching element gets added to the page?
I recall live()
being introduced in jQuery some time ago, that supported matching elements before they are created, if I understood it correctly. I never used that feature, and it now appears deprecated. But it does feel like the kind of thing I am looking for.
This is for a WordPress plugin, which uses jQuery v1.11.2 at present.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…