problem when cloning jQuery UI datepicker

后端 未结 12 1607
谎友^
谎友^ 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 05:50

    Make sure your newly cloned datepicker has a different name and ID than the original. If they both have the same name, then the behavior you're describing would be normal.

    Try changing that last line to something like:

    newDiv.find('input.datefield').attr('id', 'newDatePicker').attr('name', 'newDatePicker').datepicker();
    
    0 讨论(0)
  • 2020-11-29 05:51

    What about changing the order?

    mydiv = $('#someDiv');
    
    // clone without the events and insert
    newDiv = myDiv.clone(false).insertAfter(myDiv);
    
    // datepicker won't re-init if this class is present
    // not necessary anymore
    // newDiv.find('.hadDatepicker').removeClass('hadDatepicker');
    
    newDiv.find('input.datefield').datepicker();
    
    // datepicker attached as a last step
    mydiv.find('input.datefield').datepicker();
    

    Honestly I don't know how datepicker works internally. My hunch is that it stores something in jQuery DOM storage. Let's avoid copying it at all costs.

    (You might have long business logic between these lines of code. The point is to have a backup of #someDiv before putting a datepicker on it.)

    0 讨论(0)
  • 2020-11-29 05:57

    The complete solution that works for me.

    //before
    $('input.fecha').datepicker("destroy");
    newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    //change de input id
    $("input.fecha").attr("id", function (arr) {return "fecha" + arr;});
    //after
    $('input.fecha').datepicker({defaultDate: "+1w",changeMonth: true,numberOfMonths: 1, showOn: "button", buttonImage: "<?php echo base_url() ?>images/calendar.gif", buttonImageOnly: true, showLabel:false, dateFormat:"yy-mm-dd"});
    

    And the html td table.

    <td><?php echo form_input(array('name' => 'fecha','id' => 'fecha','class' => 'fecha')); ?></td>
    

    Hope this helps you.

    Good day

    0 讨论(0)
  • 2020-11-29 05:59

    If you call .datepicker('destroy').removeAttr('id') before cloning and then re-init the date picker it will work.

    This seems to be a bug with destroy since it is supposed to return the element back to it's original state.

    0 讨论(0)
  • 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

    <input type="text" name="ndate[]" id="date1" class="n1datepicker">
    

    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

    0 讨论(0)
  • 2020-11-29 06:02

    Here's the problem. datepicker creates UUID-based ID attributes for the input fields it binds when you initialize it. You cloning those elements results in more elements with either the same ID (which jQuery does not like) or a different ID if your clone routine manages that (which means datepicker does not know about the clones). In other words, datepicker only initializes all the elements matching your selector at the time you call it. it actually makes less sense to try to destroy/disable/enable over and over, when you can just wrap the init call inside whatever function you use to create the clones.

    Because my clone functions typically copy from hidden DOM elements rather than visible ones, I have the luxury deciding whether I need to bind before or after cloning. So, make #templateDiv a hidden element on your page with the INPUT element already in there.

    mydiv = $('#templateDiv');
    mydest = $('#someDiv');
    
    function make_the_donuts() {
        newDiv = myDiv.clone(true).insertAfter(mydest);  
        // attach datepickers by instance rather than by class
        newDiv.find('input.datefield').datepicker();
    }
    

    and that pretty much does it. Clone(true) whenever you can, it'll save you headaches in the long run.

    0 讨论(0)
提交回复
热议问题