繁体   English   中英

jquery datepicker不适用于克隆元素

[英]jquery datepicker not working for cloned elements

我有一个jquery日期选择器字段,当用户单击“ Add按钮时,我将克隆该字段。 我希望日期选择器显示在屏幕上随后添加的字段中。 现在,datepicker仅出现在第一个字段中,而not for the added/cloned fields

在这里检查了很多关于类似主题的帖子后,我能够在这个阶段达成目标。 以下是我的代码。

<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:

// 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();
   });

谢谢。

您需要在新创建的元素上初始化datepicker插件。 尝试在return false;之前添加此行return false;

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

在点击功能中初始化日期选择器..

$('.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