This question already has an answer here:
How to attach jQuery UI datepicker to dynamically inserted formfield?
                            
                                (1个答案)
                            
                    
                4年前关闭。
        

    

我正在尝试为每个动态添加的文本框访问datepicker()。
这是我的代码:

  <div id="parent">
     Date: <input type="text" class="datepicker"><input type="button"value="Add"  id="btn">
  </div>





 $(function() {
$( ".datepicker" ).datepicker();
 });

  $(document).ready(function () {

    $("#btn").click(
        function () {
            AlertSave();
        }
    );
});

  function AlertSave() {
  var textBox = document.createElement("input");
  textBox.setAttribute("class","datepicker");
document.getElementById("parent").appendChild(textBox);


   }


但是我的问题是,第一个texbox显示datepicker。但是新添加的文本字段不显示日期选择器。我的代码有什么问题?

小提琴:https://jsfiddle.net/nguc6y4L/

最佳答案

您只需要在添加新元素后初始化datepicker。
在添加新文本框后只需添加$( ".datepicker" ).datepicker();。会的。

检查:https://jsfiddle.net/koqppkuf/

关于javascript - 动态添加的文本框的Datepicker ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36786616/

10-09 18:01
查看更多