【问题标题】:php javascript - add remove dynamic form (with datepicker input)php javascript - 添加删除动态表单(使用日期选择器输入)
【发布时间】:2017-07-10 04:01:57
【问题描述】:

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

有人可以通过输入可以克隆的内容来更新我可以使用 datepicker 的代码吗?有查询界面吗?

//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 // 我可以克隆和使用。

请有人帮我看看它有效吗?

【问题讨论】:

    标签: javascript php clone


    【解决方案1】:

    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>

    【讨论】:

    • @Peisi 在您的代码中发现了一个错误... ID 不应该只是数字。即使在初始化之后,为 datepicker 设置 ID 也会导致错误,因为 DatePicker 将设置它自己的 ID。现在的问题是 - 您是否需要自己的自定义 ID 来区分输入字段?我建议改用输入名称。
    • 我将使用表单将输入保存在 mysql 数据库中。我明天上班可以试试。 Java Script 对我来说并不容易。你的意思是我不需要身份证来做什么?
    • 好的,非常感谢!我明天在工作中尝试你的解决方案并给你反馈
    • 嘿@Adam K。你的解决方案对我有用 =) 非常感谢
    • 嘿@Adam K. 现在好吗?我有勾号 =)
    【解决方案2】:

    HTML5 DatePicker 解决方案

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


    如果您想要 100% 工作的日期选择器,您必须查看 JqueryUI

    如果您希望 JqueryUI datepicker 使用克隆的元素,则必须在克隆后将新的日期输入字段初始化为 datepicker 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>

    【讨论】:

    • 嘿谢谢你能告诉我它是如何与查询 ui 一起工作的吗?以我的代码为例?
    • @Peisi 添加 JqueryUI 解决方案
    • 嘿,我的朋友,我使用您的解决方案。但是现在我发现我插入到表代码中的错误。你能告诉我为什么只有你的代码的第一部分和最后一部分进入我的 mysql 表吗?我使用“foreach $_POST as $value
    • 你好@Peisi - 使用 Chrome 中的开发工具来确定你真正从客户端发送了什么。然后您可以确定您的错误是在客户端还是服务器端。 Chrome - 开发工具 - 网络 - 文档请求 - 单击 - 并发现标头值...如果所有数据都已发送 - 您的问题出在您的服务器代码中,如果数据丢失 那么:您的输入是动态的 - 如果您使用 POST 通过javascript(不是html表单),那么你必须在按下提交btn的那一刻循环动态输入......我的猜测是你在页面加载时初始化输入 - 让动态输入丢失。 (我的猜测)
    • @Peisi 你的意思是像var section = template. 更改为var section = $('#sections .section:last-of-type').clone(). ?请记住,当没有剩余部分时,您必须放一个 if 子句才能使用模板。
    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 2017-07-15
    • 2011-07-20
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多