Cloned Select2 is not responding

前端 未结 13 2360
长发绾君心
长发绾君心 2020-11-29 03:51

I am trying to clone a row which contains select2 tool ,when i clone that row using jQuery the cloned select2 is not responding.In image given below first select2 which is o

13条回答
  •  温柔的废话
    2020-11-29 04:42

    What worked for me, to clone select input managed by select2 I did the following:
    1. Destroy the select that is cloned
    2. Clone with a true param
    3. Remove from the clone attributes 'id' and 'data-select2-id'
    4. Remove attribute 'data-select2-id' from every option in the clone
    5. Re-initialize element that was cloned
    6. Initialize cloned element resetting the value

    Here is an example:

    const origin = $('select').last(); // last in case there are more than one select
    origin.select2('destroy');
    const cloned = origin.clone(true);
    cloned.removeAttr('data-select2-id').removeAttr('id');
    cloned.find('option').removeAttr('data-select2-id');
    origin.select2();
    cloned.select2().val(null).trigger('change');
    

提交回复
热议问题