克隆div并更改克隆div的所有元素的id

时间:2017-05-10 08:41:11

标签: javascript jquery html django

我正在使用一个django项目,部分要求是在html页面上有一个按钮,当点击它时克隆一个特定的div并将其附加到页面底部,如屏幕截图所示: Screenshot of the Page

我成功地应用了以下代码:

 var vl_cnt =1; //Initial Count
    var original_external_int_div = document.getElementById('ext_int_div_1'); //Div to Clone
    function addVL(){
        var clone = original_external_int_div.cloneNode(true); // "deep" clone
        clone.id = "ext_int_div_" + ++vl_cnt; // there can only be one element with an ID
        original_external_int_div.parentNode.append(clone);
        var cloneNode = document.getElementById(clone.id).children[0].firstElementChild.firstElementChild;
        cloneNode.innerText = "External Interface "+vl_cnt;  //Change the Header of the Cloned DIV 
        $(clone).find('input:text').val('') //Clear the Input fields of the cloned DIV        
        document.getElementById("vl_count").value = vl_cnt; //Keep track of the number of div being cloned
        window.scrollTo(0,document.body.scrollHeight);

    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="height:0px;clear:both"></div>
<div style="float:right">

    <span class="label label-primary" id="add_cp_button" style="cursor: pointer;" onclick="addVL()">+ Add VL </span>

</div>
<div style="height:0px;clear:both"></div>
<div>
    <div id="ext_int_div_1">
        <div class="section">

            <fieldset class="scheduler-border">
                <legend class="scheduler-border">External Interface 1</legend>
                <div   class="sectionContent" style="border:0px solid grey;width:75%">
                    <div style="height:15px;clear:both"></div>

                    <div class="form-group">
                        <div class="col-sm-4">
                            <label>Name</label>
                        </div>
                        <div class="col-sm-8">
                            <input type="text" class="form-control"   name="vl_name_1" id="vl_name_1" placeholder="Name"/>
                        </div>
                    </div>
                    <div style="height:15px;clear:both"></div>

                    <div class="form-group">
                        <div class="col-sm-4">
                            <label>Connectivity Type</label>
                        </div>
                        <div class="col-sm-8">
                            <select class="form-control" name="vl_connectivity_type_1" id="vl_connectivity_type_1">
                                <option value="VIRTIO">VIRTIO</option>
                                <option value="">None</option>
                            </select>

                        </div>
                    </div>
                    <div style="height:15px;clear:both"></div>

                    <div class="form-group">
                        <div class="col-sm-4">
                            <label>Connection point Ref</label>
                        </div>
                        <div class="col-sm-8">
                            <select class="form-control" name="vl_con_ref_1" id="vl_con_ref_1" />
                            </select>
                        </div>
                    </div>
                    <div style="height:15px;clear:both"></div>

                </div>
            </fieldset>
            <div style="height:2px;clear:both;"></div>

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


<input type="hidden" name="vl_count" id="vl_count" value="1" />

现在我有一个新问题,我需要确保带有DIV的元素的ID也是唯一的,例如第一个输入框的ID =“vl_name_1”必须更改为“vl_name_2”创建克隆。

我尝试了以下example并在我的addVL()函数中添加了剪切,以查看我的div是否发生了任何更改:

$("#ext_int_div_1").clone(false).find("*[id]").andSelf().each(function() { $(this).attr("id", $(this).attr("id") + clone.id); });

然而,上面的代码没有给我任何东西(我很确定上面的代码是垃圾,因为我不知道它在做什么)。

在此帮忙表示赞赏。 谢谢

2 个答案:

答案 0 :(得分:0)

我希望下面的代码段有用。

&#13;
&#13;
$(document).ready(function () { 
  $sharerCount = 1;

  $('#addSharer').click(function() {
    if($sharerCount < 5) {
      $('#sharer_0').clone().attr('id', 'sharer_' + $sharerCount).insertAfter('.sharers:last').find("*[id]").attr('id', 'input_' + $sharerCount).val("").clone().end();
      $sharerCount += 1;
    }
    else {
      $('#addSharer').prop('disabled', 'true');
    }
  });
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="form">
  <div class="form-group">
    <label class="control-label col-sm-3 col-xs-12">Share With<span class="red-text">*</span></label>

    <div class="col-sm-9 col-xs-12">
      <div id="sharer_0" class="field-group sharers">
        <input id="input_0" type="text" class="form-control field-sm">
      </div>
    </div>
  </div>

  <div class="form-group"> 
    <div class="col-sm-offset-3 col-sm-9 col-xs-12">
      <button id="addSharer" type="button" class="btn btn-success">Add Another</button>
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我做了以下事情并解决了我的问题:

var vl_cnt =1; //Initial Count
var original_external_int_div = document.getElementById('ext_int_div_1'); //Div to Clone
    function addVL(){
        var clone = original_external_int_div.cloneNode(true); // "deep" clone
        clone.id = "ext_int_div_" + ++vl_cnt; // there can only be one element with an ID
        original_external_int_div.parentNode.append(clone);
        var cloneNode = document.getElementById(clone.id).children[0].firstElementChild.firstElementChild;
        cloneNode.innerText = "External Interface "+vl_cnt;  //Change the Header of the Cloned DIV
        $(clone).find("*[id]").each(function(){
           $(this).val('');
           var tID = $(this).attr("id");
           var idArray = tID.split("_");
           var idArrayLength = idArray.length;
           var newId = tID.replace(idArray[idArrayLength-1], vl_cnt);
           $(this).attr('id', newId);
        });
        document.getElementById("vl_count").value = vl_cnt; //Keep track of the number of div being cloned
        window.scrollTo(0,document.body.scrollHeight);

感谢@ProblemChild给我指向正确方向的指针,我无法提升@ProblemChild提供部分解决方案。

相关问题