【问题标题】:how to add the date field in a form using addmore function javascript如何使用addmore函数javascript在表单中添加日期字段
【发布时间】:2014-07-26 06:49:20
【问题描述】:

我必须使用添加更多图像来添加多个日期字段,但是当我单击“日期字段”时会出现日历。

但是当我添加更多日期字段时(日期 2,日期 3...)。日历不会出现在该日期字段中。

firebug 中没有错误。

我的代码在这里。请参阅附件。日历未显示在“日期 2”文本字段中。

    <script>
$(function() {
    $(".dp").datepicker();

  });

    fields = 2;
    function addInput() 
    {    
        document.getElementById('text').innerHTML += "<div class='form-group' id='group_"+fields+"'><label class='col-sm-3 col-md-3 col-lg-2 control-label'>Date "+fields+":</label>  <input id='date_"+fields+"' class='dp' type='text' placeholder='Date'>    <input type='image' value='Remove'   src='img/remove.png' onclick='removerow("+fields+");' height='25px'></div></div>";
        document.getElementById('count_off').value=fields;
        fields += 1;     
    }
    </script>   

    <div>
        <input id='date_1' class='dp' type='text' placeholder='Date'>  
        <a href="javascript:void(0);" id="addButton"  onclick="addInput();">
        <img src="img/addmore.png" width="" height="25px"></a>  
    </div>

    <div id="text" ></div>

【问题讨论】:

    标签: javascript jquery html date


    【解决方案1】:

    Id 分配给所有日期选择器字段可能会给您带来处理问题。因此最好分配类并在类的帮助下完成所有工作。我刚刚编写了添加日期选择器的代码。

    $(".dp").datepicker();
    
    $(document).on('click', '.add', function() {
        var appendTxt = '<input class="dp"  type="text" /><button class="add">Add</button>';
        $("#container").append(appendTxt);
        $(".dp").datepicker();
    
    });
    

    HTML 是

    <div id="container">
    <input class="dp" type="text"></input><button class="add">Add</button>
    </div>
    

    SEE DEMO HERE

    【讨论】:

      【解决方案2】:

      像这样在你的函数addInput中应用jquery函数$(".dp").datepicker();

      function addInput() 
          {    
              document.getElementById('text').innerHTML += "<div class='form-group' id='group_"+fields+"'><label class='col-sm-3 col-md-3 col-lg-2 control-label'>Date "+fields+":</label>  <input id='date_"+fields+"' class='dp' type='text' placeholder='Date'>    <input type='image' value='Remove'   src='img/remove.png' onclick='removerow("+fields+");' height='25px'></div></div>";
              document.getElementById('count_off').value=fields;
      
              $(".dp").datepicker(); // add this line
      
          }
      

      更新 2:

      function addInput() 
          {    
              document.getElementById('text').innerHTML += "<div class='form-group' id='group_"+fields+"'><label class='col-sm-3 col-md-3 col-lg-2 control-label'>Date "+fields+":</label>  <input id='date_"+fields+"' class='dp' type='text' placeholder='Date'>    <input type='image' value='Remove'   src='img/remove.png' onclick='removerow("+fields+");' height='25px'></div></div>";
              document.getElementById('count_off').value=fields;
      
              $("date_"+fields).datepicker(); // add this line
              fields += 1;
          }
      

      【讨论】:

      • 感谢 sathish。如果我们添加 Date2、Date3、Date4 字段,日历仅出现在日期 4 中。日期 2、日期 3 不显示。这意味着最后添加的字段仅显示日历。任何其他解决方案都可以解决
      • 另一个想法是使用唯一的id 而不是类.dp 试试我的更新2
      • 你能不能把这个放到js fiddle里。我认为问题是其他的。
      猜你喜欢
      • 2021-08-11
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      相关资源
      最近更新 更多