6

Add我有一个 jquery 日期选择器字段,当用户单击按钮时我正在克隆它。我希望日期选择器出现在屏幕上随后添加的字段中。现在日期选择器只出现在第一个字段和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();
   });

谢谢。

4

2 回答 2

5

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

newSection.find(".dateListValues").datepicker();
于 2012-12-14T10:08:00.710 回答
1

在点击函数中初始化日期选择器..

$('.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;
});
于 2012-12-14T10:10:05.380 回答