简体   繁体   English

动态创建的输入上的jQuery datepicker更改第一个输入的日期

[英]Jquery datepicker on dynamically created inputs changing the date of the first input

I am adding date inputs with datepicker attached. 我要添加带有日期选择器的日期输入。

An the problem is: I pick a date in the first input and I try to pick another date in the second or third or .. inputs. 一个问题是:我在第一个输入中选择一个日期,然后尝试在第二个或第三个或..输入中选择另一个日期。 Well, this last picked date shows in the first input, overwriting the already picked date, and leaves the new input blank. 好吧,这个最后选择的日期显示在第一个输入中,将覆盖已经选择的日期,并将新输入保留为空白。

Why is that? 这是为什么? How can I solve this? 我该如何解决?

Here's the html: 这是html:

    <div id="main">
    <span>
    <input type="text" id="mydate" name="mydate[]" class="datepicker">
    </span>
    </div>
    <a href="#" id="addInput">Add input</a>

And here's the js: 这是js:

    var datePickerOptions = {
        dateFormat: 'd/m/yy',
        firstDay: 1,
        changeMonth: true,
        changeYear: true
    }

    $(document).ready(function() 
    {  
      $('.datepicker').datepicker(datePickerOptions);

    }); 

    $(function() {
    var scntDivA = $('#main');
    var ii = $('#main span').size() + 1;

            $('#addInput').live('click', function() {

                    $('<input type="text" id="mydate" name="mydate[]" class="datepicker"').appendTo(scntDivA);
                    $('.datepicker').datepicker(datePickerOptions);
                    ii++;
                    return false;
            });
   }); 

Thanks! 谢谢!

Change this code: 更改此代码:

$('<input type="text" id="mydate" name="mydate[]" class="datepicker"').appendTo(scntDivA);
$('.datepicker').datepicker(datePickerOptions);

to

var inp =   $('<input type="text" id="mydate" name="mydate[]" class="datepicker"').appendTo(scntDivA);
inp.datepicker(datePickerOptions);

When you use .appendTo it returns the item that was appended, in this case the new input, so you don't need to "re-find" it. 当您使用.appendTo它将返回附加的项目,在本例中为新输入,因此您无需“重新查找”它。

When you use $('.datepicker'). 当您使用$('.datepicker'). it finds everything with class .datepicker including all the previously added items, thus applying the new options to the old datepicker. 它会找到类.datepicker的所有内容,包括以前添加的所有项目,从而将新选项应用于旧的datepicker。

You can check below code. 您可以检查以下代码。 We are setting the unique class to each newly added text field and initializing the datepicker on newly added textfield only. 我们将为每个新添加的文本字段设置唯一类,并仅在新添加的文本字段上初始化日期选择器。 In your code you were re-initializing the existing textfields hence previously selected dates were getting overridden. 在您的代码中,您正在重新初始化现有的文本字段,因此先前选择的日期将被覆盖。

 var datePickerOptions = { dateFormat: 'd/m/yy', firstDay: 1, changeMonth: true, changeYear: true } $(document).ready(function() { $('.datepicker').datepicker(datePickerOptions); var scntDivA = $('#main'); var ii = $('#main span').size() + 1; $('#addInput').on('click', function() { //adding the unique class like datepicker_number var newText = $("<input type=\\"text\\" id=\\"mydate_"+ii+"\\" name=\\"mydate[]\\" class=\\"datepicker datepicker_"+ii+"\\">"); $(newText).appendTo(scntDivA); //initializing the datepicker only on newly added textfield with class like datepicker_number $('.datepicker_'+ii).datepicker(datePickerOptions); ii++; return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.css" /> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script> <div id="main"> <span> <input type="text" id="mydate" name="mydate[]" class="datepicker"> </span> </div> <a href="#" id="addInput">Add input</a> 

Note: above code snippet uses jQuery 1.11.0 but you can use code within .on handler as it is within your .live code. 注意:以上代码段使用jQuery 1.11.0,但您可以在.on处理程序中使用代码,因为它位于.live代码中。

This uses jquery 1.9.1 这使用jQuery 1.9.1

$(document).ready(function() 
{  
  $('body').on('focus',".datepicker", function(){
            $(this).datepicker(datePickerOptions);
    });​
}); 

Working Fiddle : https://jsfiddle.net/khairulhasanmd/bLb047rL/2/ 工作小提琴: https//jsfiddle.net/khairulhasanmd/bLb047rL/2/

Just call the datepicker plugin once for all initial inputs and once for every newly created input. 只需为所有初始输入调用一次datepicker插件,然后为每个新创建的输入调用一次。

http://codepen.io/kante/pen/ALRvqN http://codepen.io/kante/pen/ALRvqN

Html: HTML:

<div id="main">
  <span>
    <input type="text" id="mydate" name="mydate[]" class="datepicker">
  </span>
</div>
<a href="#" id="addInput">Add input</a>

JS: JS:

var datePickerOptions = {
  dateFormat: 'd/m/yy',
  firstDay: 1,
  changeMonth: true,
  changeYear: true
}

$(document).ready(function() {
  $('.datepicker').datepicker();
  $('#addInput').live('click', function(){
    $input = $('<input type="text" name="mydate[]" />').datepicker(datePickerOptions);
    $('<div>').html($input).appendTo($('#main'));
  });
}); 

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

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