关于克隆元素的jquery datepicker [英] jquery datepicker on cloned elements

查看:226
本文介绍了关于克隆元素的jquery datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to apply a datepicker control to cloned input fields. What I'm doing is finding the table row I want to clone, clone it with clone(false) and then for each input with a class .date call datepicker() on them. The code is as follows:

$('.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位。
任何人都有任何想法?

Now both $(this).datepicker() and $('.date').datepicker() fail to attach a datepicker control to input.date. The above code works as expected except for the datepicker bit. Anybody have any ideas?!

提前感谢!

推荐答案

如果您先添加行(应用datepicker )之前似乎有效,并删除由datepicker添加的类 .hasDatepicker

Seems to work if you add the row first (before applying the datepicker) and also remove the class added by the datepicker .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/

这篇关于关于克隆元素的jquery datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆