Cloned Select2 is not responding

前端 未结 13 2430
长发绾君心
长发绾君心 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条回答
  •  猫巷女王i
    2020-11-29 04:22

    I faced the same problem, while trying to add a row to a table dynamically. (the row contains more than one select2 instance.)

    I solved it in this way:

    function addrow()
    {
        var row = $("#table1 tr:last");
    
        row.find(".select2").each(function(index)
        {
            $(this).select2('destroy');
        }); 
    
        var newrow = row.clone();       
    
        $("#table1").append(newrow);
    
        $("select.select2").select2();
    }
    

    The trick was that you need to destroy all the instances of .select2 separately and before cloning the row. And then after cloning, re-initialize the .select2. I hope this will work for others as well.

提交回复
热议问题