php javascript - 添加删除动态表单(带日期选择器输入)

php javascript - add remove dynamic form (with datepicker input)

我使用一些代码来克隆表单元素,但我不知道它如何与日期选择器一起使用。

有人可以更新我可以通过输入什么可以克隆来使用日期选择器的代码吗?使用查询 ui?

//define template
var template = $('#sections .section:first').clone();

//define counter
var sectionsCount = 1;

//add new section
$('body').on('click', '.addsection', function() {

    //increment
    sectionsCount++;

    //loop through each input
    var section = template.clone().find(':input').each(function(){

        //set id to store the updated section number
        var newId = this.id + sectionsCount;

        //update for label
        $(this).prev().attr('for', newId);

        //update id
        this.id = newId;

    }).end()

    //inject new section
    .appendTo('#sections');
    return false;
});

//remove section
$('#sections').on('click', '.remove', function() {
    //fade out section
    $(this).parent().fadeOut(300, function(){
        //remove parent element (main section)
        $(this).parent().parent().empty();
        return false;
    });
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
   <div id="sections">
  <div class="section">
    <fieldset>
        <legend>test form - <a href="#" class='button red remove'>delete</a></legend>
        <p>
            <label for="number1">number1:</label>
            <input type="text" name="number1[]"/>
        </p>
  
  <p>
<label for="number2">number2:</label>
            <input type="text" name="number2[]"/>
        </p>
  
  <p>
            <label for="selcet1">selcet1:</label>
  <select name="select1[]" required>
  <option value="1" >1</option>
    <option value="2" >2</option>
  </select>
        </p>

        <p>
<label for="selcet2">selcet2:</label>
        <select name="selcet2[]" required>
   <option value="1" >1</option>
      <option value="2" >2</option>
   </select>
        </p>


  <p>
<label for="date">Date:</label>
 <input type="text" name="date[]" required /> 
        </p>
  <p>
            <label for="text">text:</label>
<textarea rows=3 type="text" name="text[]" > </textarea>
        </p>

      
    </fieldset>
 
  </div>
</div>

       <a href="#" class='addsection'>add form</a>

我不知道如何通过 // input type="text" name="date[]" required // 我可以克隆和使用。

有人可以帮我看看它有效吗?

HTML5 DatePicker 解决方案

本机 HTML5 日期选择器仅在现代浏览器中受支持,甚至并非所有浏览器都受支持。 只需使用 type="date" 而不是 type="text"


如果你想要 100% 工作的日期选择器,你将不得不查看 JqueryUI

如果您希望 JqueryUI 日期选择器使用克隆的元素,您必须在克隆后将新的日期输入字段初始化为日期选择器 jquery 输入元素。


//define template
var template = $('#sections .section:first').clone();

//define counter
var sectionsCount = 1;

//add new section
$('body').on('click', '.addsection', function() {

    //increment
    sectionsCount++;

    //loop through each input
    var section = template.clone().find(':input').each(function(){

        //set id to store the updated section number
        var newId = this.id + sectionsCount;

        //update for label
        $(this).prev().attr('for', newId);

        //update id
        this.id = newId;

    }).end()

    //inject new section
    .appendTo('#sections');
    return false;
});

//remove section
$('#sections').on('click', '.remove', function() {
    //fade out section
    $(this).parent().fadeOut(300, function(){
        //remove parent element (main section)
        $(this).parent().parent().empty();
        return false;
    });
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
   <div id="sections">
  <div class="section">
    <fieldset>
        <legend>test form - <a href="#" class='button red remove'>delete</a></legend>
        <p>
            <label for="number1">number1:</label>
            <input type="text" name="number1[]"/>
        </p>
  
  <p>
<label for="number2">number2:</label>
            <input type="text" name="number2[]"/>
        </p>
  
  <p>
            <label for="selcet1">selcet1:</label>
  <select name="select1[]" required>
  <option value="1" >1</option>
    <option value="2" >2</option>
  </select>
        </p>

        <p>
<label for="selcet2">selcet2:</label>
        <select name="selcet2[]" required>
   <option value="1" >1</option>
      <option value="2" >2</option>
   </select>
        </p>


  <p>
<label for="date">Date:</label>
 <input type="date" name="date[]" required /> 
        </p>
  <p>
            <label for="text">text:</label>
<textarea rows=3 type="text" name="text[]" > </textarea>
        </p>

      
    </fieldset>
 
  </div>
</div>

       <a href="#" class='addsection'>add form</a>

JqueryUI解决方案

//define template
var template = $('#sections .section:first').clone();

//define counter
var sectionsCount = 1;

//add new section
$('body').on('click', '.addsection', function() {

    //increment
    sectionsCount++;

    //loop through each input
    var section = template.clone().find(':input').each(function(){

        //set id to store the updated section number
        var newId = this.id + sectionsCount;

        //update for label
        $(this).prev().attr('for', newId);

        //update id - THIS CAUSES JqueryUI Datepicker to bug, also you shouldn't use numerical only IDs
        //this.id = newId;

    }).end()

    //inject new section
    .appendTo('#sections');
    //initialize datePicker on last name=date[] element in HTML DOM
    $("input[name='date[]']").last().datepicker();
    return false;
});
//init original datePicker in HTML DOM
$("input[name='date[]']").last().datepicker();
//remove section
$('#sections').on('click', '.remove', function() {
    //fade out section
    $(this).parent().fadeOut(300, function(){
        //remove parent element (main section)
        $(this).parent().parent().empty();
        return false;
    });
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
   
   <div id="sections">
  <div class="section">
    <fieldset>
        <legend>test form - <a href="#" class='button red remove'>delete</a></legend>
        <p>
            <label for="number1">number1:</label>
            <input type="text" name="number1[]"/>
        </p>
  
  <p>
<label for="number2">number2:</label>
            <input type="text" name="number2[]"/>
        </p>
  
  <p>
            <label for="selcet1">selcet1:</label>
  <select name="select1[]" required>
  <option value="1" >1</option>
    <option value="2" >2</option>
  </select>
        </p>

        <p>
<label for="selcet2">selcet2:</label>
        <select name="selcet2[]" required>
   <option value="1" >1</option>
      <option value="2" >2</option>
   </select>
        </p>


  <p>
<label for="date">Date:</label>
 <input type="text" name="date[]" required /> 
        </p>
  <p>
            <label for="text">text:</label>
<textarea rows=3 type="text" name="text[]" > </textarea>
        </p>

      
    </fieldset>
 
  </div>
</div>

       <a href="#" class='addsection'>add form</a>