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

标签 javascript php clone

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

有人可以更新我可以通过输入可以克隆的内容来使用日期选择器的代码吗?使用查询用户界面?

//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>

我不知道如何添加日期选择器 //输入 type="text"name="date[]"必需//我可以克隆和使用。

请有人帮我看看它是否有效?

最佳答案

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>

关于javascript - php javascript - 添加删除动态表单(带有日期选择器输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42351943/

相关文章:

javascript - 如何计算在 Javascript 中以字符串形式给出的 64 位整数的除法余数

php - mysqli_num_rows 为 1 但 mysql_fetch_assoc 返回 null

php - 为什么 trim() 会使一个不存在的变量存在?

c - 什么是列表克隆以及如何克隆?

javascript - 事件参数未传递给 JavaScript 函数

javascript - JQuery 事件处理程序未通过面包屑链接附加

javascript - jQuery 替换文本和格式

javascript - 如何只提交一次jquery ajax post请求?

mysql - 在另一台服务器上有效地克隆 MySQL 数据库

java - 如何在 CLONE JPanel 中使用 paint()?