单击加号按钮后,如何重复获得相同数量的div?

时间:2018-07-24 04:40:29

标签: javascript php jquery html

我正在使用php开发我的网站。我有一个<div>,其中包含多个输入字段。现在,我的问题是,一旦我单击加号按钮,如何重复重复同一<div>

<div id='testdiv' class='testclass'>
    <form name="myForm" method="post" action="#" onsubmit="return validateForm();" autocomplete="off" enctype="multipart/form-data">
            <input class="btn btn-success col-sm-1" type="submit" name="submit"  id="submit" value="Save"/>
            <div>
                <h3 style="color:#1E90FF;"><center><b>My name</b></center></h3><br/>
                <div class="col-sm-7">
                    <div class="col-sm-2">
                        <label id="test_lable" for="">my address:</label>
                    </div>
                    <div class="col-sm-6">
                        <input  class="form-control input-sm" type="text" name="testname" id="testname" required /><br/>
                    </div>
                </div>
                <div class="col-sm-7">
                    <div class="col-sm-2">
                        <label><b>place</b></label>
                    </div>
                </div>
                <div class="col-sm-7">
                    <div class="col-sm-2">
                        <label><b>user Type</b></label>
                    </div>
                </div>
                    <div ng-controller="AppCtrl" ng-cloak="" class="col-sm-7" ng-app="MyApp" style= "background-color: white">
                    <div class="col-sm-2" style="padding:29px;">
                            <label><b>Instance/s</b></label>
                    </div>
                    <div class="col-sm-6">
                        <md-content style="padding:10px; background-color: white">
                        <md-slider-container>
                              </md-slider>
                              <md-input-container>
                              </md-input-container>
                            </md-slider-container>
                         </md-content>
                    </div>

                        <div class="col-sm-7">
                            <div class="row">
                                <div class="col-xs-8 col-xs-offset-2 well">
                                    <legend>Select File to Upload:</legend>
                                    <div class="form-group">
                                        <input type="file" name="file1" />
                                    </div>
                                </div>
                            </div>
                        </div>
                </div>
                </div>
            </div>
        </form>
        </div>

2 个答案:

答案 0 :(得分:0)

您必须在jquery中使用.clone()克隆整个div。然后单击按钮,您必须将其附加到所需的位置。

选中此fiddle

或者,如果您有一个预定义的代码要在单击按钮时重复,那么您可以执行以下操作。

在php

function get_repeat_html(){
  ?>
  <div id="get_repeat_html" style="display:none;">
      <div class="box" id="101">
        <input type="text" value="text">
        <p>Sample text</p>
      </div>
  </div>
  >?php
}

在js

$('.btn').click(function(){
    var html = $('#get_repeat_html').html();
    $('#target').append(html);
});

答案 1 :(得分:0)

以下div会根据您的点击重复

HTML

 <div class="input_fields_wrap">
        <button class="add_field_button">Add More Fields</button>
        <div><input type="text" name="mytext[]"></div>
    </div>

jQuery

$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

https://xmpp.org/rfcs/rfc3921.html#presence]