简体   繁体   English

我想在 JavaScript 的克隆方法中更改 label 值

[英]I want to change label value in clone method in JavaScript

I am using JavaScript to clone a row of form by clicking a button.我正在使用 JavaScript 通过单击按钮克隆一行表单。 And each time a row is added, the label value of the new row change.并且每增加一行,新行的label值就会改变。 I mean the main label is "dependent 2" and in new row it is change to "dependent 3".I do not know how to change the label.我的意思是主要的 label 是“从属 2”,在新行中它更改为“从属 3”。我不知道如何更改 label。 here you can see my code.在这里你可以看到我的代码。 the clone is working but without changing the label:克隆正在工作,但没有更改 label:

 function myFunction() { var elem = document.querySelector('#dependent'); var clone = elem.cloneNode(true); clone.id = "dependent2"; elem.after(clone); var param = document.getElementsByTagName("lable"); var id = new Array(); for (i = 2; i < 7; i++) { id[i] = param.item(i); lable[i].innerHTML = "dependent" + i + 1; } }
 <form class="needs-validation" invalidate> <div class="form-row"> <label class="col-4 offset-2 col-form-label">Dependent 1</label> <div class="form-group col-4"> <div class="input-group mx-auto" style="border-radius: 3px;width:100%;"> <input type="number" class="form-control " id="validationDefault01" style="font-size:15px" placeholder="Age" required> </div> </div> </div> <div class="form-row" id="dependent"> <label class="col-4 offset-2 col-form-label">Dependent 2</label> <div class="form-group col-4"> <div class="input-group mx-auto" style="border-radius: 3px;width:100%;"> <input type="number" class="form-control " id="validationDefault01" style="font-size:15px" placeholder="Age" required> </div> </div> </div> <button class="quebtn1 mx-auto" type="button" style="width: 150px;border-radius:8px;" onClick="myFunction()">Add Dependents</button>

Apart from the typo (lable / label), you seemed to be over complicating the updating of the label text (why the loop?).除了错字(标签/标签)之外,您似乎过于复杂了 label 文本的更新(为什么是循环?)。 This can be done simply:这可以简单地完成:

 let nextLabel = 3; function myFunction() { var elem = document.querySelector('#dependent'); var clone = elem.cloneNode(true); clone.id = "dependent"+nextLabel; elem.parentElement.insertBefore(clone,elem.parentElement.children[elem.parentElement.length-1]); var label = clone.querySelector("label") label.innerHTML = "Dependent " + (nextLabel++); }
 <form class="needs-validation" invalidate> <div class="form-row"> <label class="col-4 offset-2 col-form-label">Dependent 1</label> <div class="form-group col-4"> <div class="input-group mx-auto" style="border-radius: 3px;width:100%;"> <input type="number" class="form-control " id="validationDefault01" style="font-size:15px" placeholder="Age" required> </div> </div> </div> <div class="form-row" id="dependent"> <label class="col-4 offset-2 col-form-label">Dependent 2</label> <div class="form-group col-4"> <div class="input-group mx-auto" style="border-radius: 3px;width:100%;"> <input type="number" class="form-control " id="validationDefault01" style="font-size:15px" placeholder="Age" required> </div> </div> </div> <button class="quebtn1 mx-auto" type="button" style="width: 150px;border-radius:8px;" onClick="myFunction()">Add Dependents</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM