How to clone jQuery Datepicker input field

喜你入骨 提交于 2019-12-12 01:54:21

问题


I need to clone a jQuery datepicker input field attached a datePicker to it using the on statement.

$('form').on('click', '.datepicker', function(){
         $(this).datepicker({'changeMonth': true, 'changeYear': true, 'dateFormat': 'MM dd', 'yearRange': "<?php echo date('Y') ?>:<?php echo date('Y') + 1 ?>"}).focus();
    });

Then before cloning the input filed, I am destroying the datepicker using

$(".datepicker").datepicker('destroy').removeClass('hasDatepicker');

But still the datepicker that is being attached to then newly cloned input is messing with original input field...

Can anybody please help me on this topic?


回答1:


try this

var $clone=$(".datepicker").clone();
$clone.datepicker("destroy");
$clone.removeAttr("id");
$clone.datepicker();
$('form').append($clone);

when you clone a row it copies every thing, and when the date picker is added again it calls the input by id, you must remove the id so that the next datepicker can assign a new id to the input :)




回答2:


You should destroy the datepicker for the clone element like,

var $clone=$(".datepicker").clone();
$clone.datepicker('destroy').removeClass('hasDatepicker');
$('form').append($clone);


来源:https://stackoverflow.com/questions/19150968/how-to-clone-jquery-datepicker-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!