克隆元素上的jquery datepicker

我正在尝试将一个datepicker控件应用于克隆的输入字段。 我正在做的是找到我要克隆的表格行,用克隆(false)克隆它,然后对每个输入使用类.date调用datepicker()。 代码如下:

$('.repeat').bind('click', function(){ var parentEl = $(this).parents('.root'); var lastRow = jQuery.makeArray($(parentEl).find('.last')); var newRow = $(lastRow).clone(false); $(lastRow).removeClass('last'); $(newRow).addClass('last'); newRow.find('input').each(function(){ this.name = this.name.replace(/\[(\d+)\]/,function(str,p1){return '[' + (parseInt(p1,10)+1) + ']'}); }).end().insertAfter($(lastRow)); newRow.find('.date').each(function() { $(this).removeAttr('id'); $('.date').datepicker({dateFormat: 'dd-mm-yy', changeYear: true, yearRange: '1970:2010'}); }); 

现在,$(this).datepicker()和$(’。date’)。datepicker()都无法将datepicker控件附加到input.date。 除了datepicker位之外,上面的代码按预期工作。 有人有什么想法吗?!

提前致谢!

如果您首先添加行( 在应用 .hasDatepicker 之前 )并且还删除由.hasDatepicker添加的类,则.hasDatepicker

 $('.repeat').bind('click', function(){ var parentEl = $(this).parents('.root'); var lastRow = jQuery.makeArray($(parentEl).find('.last')); var newRow = $(lastRow).clone(false, false); $(lastRow).removeClass('last'); $(newRow).addClass('last'); $('.root').append(newRow); // added this newRow.find('.date').each(function() { $(this).removeAttr('id').removeClass('hasDatepicker'); // added the removeClass part. $('.date').datepicker({dateFormat: 'dd-mm-yy', changeYear: true, yearRange: '1970:2010'}); }); }); 

演示 : http : //jsfiddle.net/gaby/LCfC2/

必须删除class元素,然后必须将日期选择器添加到克隆元素中。 无论是否已经附加到DOM,我每次都可以使用它。

 $(inputData3).removeAttr("class"); $(inputData3).datepicker();