[英]How can I clone and remove a html form with JavaScript(js)?
I want to clone a form and then have the option to remove the form that I have just created.我想克隆一个表单,然后可以选择删除我刚刚创建的表单。 I know how to clone it, but not how to remove it.我知道如何克隆它,但不知道如何删除它。 I have only be able to remove the original form, but not the clonned form.我只能删除原始表格,但不能删除克隆表格。
This is my code:这是我的代码:
function duplicate(id) { var elmnt = document.getElementById(id); var cln = elmnt.cloneNode(true); var num = 1; cln.id = id + num; document.body.appendChild(cln); } function remove(id) { var elem = document.getElementById(id); elem.parentNode.removeChild(elem); }
<form class="f0" action="action.php" id="f0"> <input type="text" name="name" id="name" required=""> <button onclick="duplicate('f0')">Duplicate</button> <button onclick="remove('f0')">Remove</button> <input type="submit" value="Next"/> </form>
You don't need id
s for this at all.你根本不需要id
。
The minimal change is to pass this
into remove
from the onclick
(and, since you don't want it to submit the form, give it a type
):最小的更改是将其传递this
从onclick
remove
(并且,由于您不希望它提交表单,因此给它一个type
):
<button onclick="remove(this)" type="button">Next</button>
Then, in remove
, use the button element that's passed in to find the form it's in:然后,在remove
,使用传入的按钮元素来查找它所在的表单:
function remove(button) {
var form = button.closest("form");
form.parentNode.removeChild(form);
}
You can make a similar change to duplicate
:您可以对duplicate
进行类似的更改:
function duplicate(button) {
var form = button.closest("form").cloneNode(true);
document.body.appendChild(form);
}
Be sure to remove the id
from the form, since it's not needed and would be duplicated by the above.请务必从表单中删除id
,因为它不是必需的,并且会被上述重复。
Live Example:现场示例:
function remove(button) { var form = button.closest("form"); form.parentNode.removeChild(form); } function duplicate(button) { var form = button.closest("form").cloneNode(true); document.body.appendChild(form); }
<form class="f0" action="action.php"> <input type="text" name="name" id="name" required=""> <button onclick="duplicate(this)" type="button">Duplicate</button> <button onclick="remove(this)" type="button">Next</button> </form>
But even better would be to not use the onclick
attribute at all, because when you do you have to use global functions, and globals are best avoided.但更好的是根本不使用onclick
属性,因为当你必须使用全局函数时,最好避免使用全局函数。 Use modern event handling ( addEventListener
) instead:改用现代事件处理( addEventListener
):
function remove() { var form = this.closest("form"); form.parentNode.removeChild(form); } function duplicate() { var form = this.closest("form").cloneNode(true); setupHandlers(form); document.body.appendChild(form); } function setupHandlers(form) { form.querySelector(".form-duplicate").addEventListener("click", duplicate); form.querySelector(".form-remove").addEventListener("click", remove); } // Setup handlers on any initially-present forms document.querySelectorAll(".f0").forEach(setupHandlers);
<form class="f0" action="action.php"> <input type="text" name="name" id="name" required=""> <button class="form-duplicate" type="button">Duplicate</button> <button class="form-remove" type="button">Next</button> </form>
Another option is to use event delegation, handling the clicks on the container element all of these forms are in ( body
in your example):另一种选择是使用事件委托,处理所有这些 forms 中的容器元素的点击(在您的示例中为body
):
document.body.addEventListener("click", function(e) { var btn = e.target.closest(".form-duplicate"); if (btn && this.contains(btn)) { duplicate(btn); return; } btn = e.target.closest(".form-remove"); if (btn && this.contains(btn)) { remove(btn); return; } }); function remove(btn) { var form = btn.closest("form"); form.parentNode.removeChild(form); } function duplicate(btn) { var form = btn.closest("form").cloneNode(true); document.body.appendChild(form); }
<form class="f0" action="action.php"> <input type="text" name="name" id="name" required=""> <button class="form-duplicate" type="button">Duplicate</button> <button class="form-remove" type="button">Next</button> </form>
Note that closest
is relatively new, you'll need a polyfill for IE.请注意, closest
的是相对较新的,你需要一个用于 IE 的 polyfill。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.