我试图将日期选择器控件应用于克隆的输入字段。 我在做的是找到我想要克隆的表行,用克隆(假)克隆它,然后为每个输入带一个类.date调用datepicker()。代码如下:关于克隆元素的jquery 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'});
});
现在无论$(本).datepicker()和$( '日')日期选择器()没有附上日期选择控件input.date。 上面的代码按照预期工作,除了datepicker位。 有人有什么想法?
在此先感谢!
您是否曾尝试在应用datepicker之前将它们附加到DOM? – 2011-04-26 10:49:12
对不起,我的错。有一些代码没有发布,它在执行了一些格式化操作后,在lastRow(insertAfter($ lastRow))之后插入了newRow。我认为那会把它附加到DOM上,还是我误会了? – jimmy 2011-04-26 11:01:36
确实会将它添加到DOM中。我发布了一个答案,您需要对代码进行额外的更改才能工作。 – 2011-04-26 11:07:54