On the initialization of dynamically generated elements by select2

When doing a project, select2, the beautification plug-in of select, is generally an easy-to-use plug-in. However, I encountered a small problem in the process of using. A group of select dynamically generated by JS was not initialized successfully, that is to say, the initialization method of select2 was not bound to the unknown element. I tried to initialize again in the trigger event, and the result was uncaught query function not defined for select2 s2id_ Stores’ fault

The reason is that the same object cannot be initialized twice, so a method is written

function attachSelect2(event){

event.find(“#lesson”+i).select2({

width:”100%”,

minimumResultsForSearch: -1

})

}

Lesson is the ID that I set for a group of select, then I + + set different ID for each select in the trigger event, and then initialize different ID, which is successful~

Similar Posts: