简体   繁体   English

jquery datepicker不适用于克隆元素

[英]jquery datepicker not working for cloned elements

I have a jquery date picker field which I am cloning when a user clicks on an Add button. 我有一个jquery日期选择器字段,当用户单击“ Add按钮时,我将克隆该字段。 I want the date picker to appear for the subsequently added fields on the screen. 我希望日期选择器显示在屏幕上随后添加的字段中。 Right now the datepicker is appearing only for the first field and not for the added/cloned fields . 现在,datepicker仅出现在第一个字段中,而not for the added/cloned fields

After checking a lot of posts on similar topic here, I was able to reach at this stage. 在这里检查了很多关于类似主题的帖子后,我能够在这个阶段达成目标。 Below is my code so far. 以下是我的代码。

<div class="repeatingSection">
<a href="#" class="deleteDate" style="display: none;">-Delete</a>
<input type="text" class="dateListValues" style="position: relative; z-index:100000;" 
       id="dateListValues_1" size="15" />
</div>
<a href="#" class="addDate">+ Add</a>

JS: JS:

// Add a new repeating section
$('.addDate').click(function(){
    var currentCount =  $('.repeatingSection').length;
    var newCount = currentCount+1;
    var newID;
    var lastRepeatingGroup = $('.repeatingSection').last();
    var newSection = lastRepeatingGroup.clone();
    newSection.insertAfter(lastRepeatingGroup);
    newSection.find("input").each(function (index, input) {
        input.id = input.id.replace("_" + currentCount, "_" + newCount);
        input.name = input.name.replace("_" + currentCount, "_" + newCount);
        input.value = "";
            //removing the additional hasDatepicker class 
        $('#'+input.id).removeClass('hasDatepicker');
    });

    return false;
});

   $('.dateListValues').each(function(){
    $(this).datepicker();
   });

Thanks. 谢谢。

You need to initialise the datepicker plugin on the newly created element. 您需要在新创建的元素上初始化datepicker插件。 Try adding this line right before your return false; 尝试在return false;之前添加此行return false; :

newSection.find(".dateListValues").datepicker();

initailize date picker inside the click function.. 在点击功能中初始化日期选择器..

$('.addDate').click(function(){
var currentCount =  $('.repeatingSection').length;
var newCount = currentCount+1;
var newID;
var lastRepeatingGroup = $('.repeatingSection').last();
var newSection = lastRepeatingGroup.clone();
newSection.insertAfter(lastRepeatingGroup);
newSection.find("input").each(function (index, input) {
    input.id = input.id.replace("_" + currentCount, "_" + newCount);
    input.name = input.name.replace("_" + currentCount, "_" + newCount);
    input.value = "";
        //removing the additional hasDatepicker class 
    $('#'+input.id).removeClass('hasDatepicker');

});
  newSection.find(".dateListValues").datepicker(); //here
  return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM