problem when cloning jQuery UI datepicker

后端 未结 12 1626
谎友^
谎友^ 2020-11-29 05:36

I have a div in which there is a datepicker. I use something like this to clone it:

mydiv = $(\'#someDiv\');

// works fine so far
mydiv.find(\'input.datefie         


        
12条回答
  •  感情败类
    2020-11-29 06:01

    This might be a little late, but all the suggestions above didn't work for me, I came up with an easy solution for this.

    First, what is causing the problem: JQuery assign datepicker to element id. if you are cloning element, then same id might be cloned as well. which jQuery doesn't like. You might end up with either receiving null reference error or the date being assigned to first input field regardless which input field you click on.

    Solution:

    1) destroy datepicker 2) assign new unique ids to all input field 3) assign datepicker for each input

    Make sure your input is something like this

    
    

    Before you clone, destroy datepicker

    $('.n1datepicker').datepicker('destroy');
    

    After you clone, add these lines as well

    var i = 0;
    $('.n1datepicker').each(function () {
        $(this).attr("id",'date' + i).datepicker();
        i++;
    });
    

    and the magic happens

提交回复
热议问题