Cloning datepicker objects [JQuery]

后端 未结 4 700
温柔的废话
温柔的废话 2021-01-02 12:25

I have an input field that I define as a datepicker through a css class. I now want to clone this input field and have it so the cloned inputs are also datepickers.

4条回答
  •  一整个雨季
    2021-01-02 12:42

    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

提交回复
热议问题