[英]Small Javascript Issue with removing multiple elements from dynamic form
如果可能的话,任何人都可以阐明我在做什么错吗?
我们在php页面上有一个带有javascript的基本表单,当单击按钮时会创建4个表单字段。
如果多次单击该按钮,它将继续创建4列的行。 问题是如果有人单击该按钮,则试图删除该行(4个元素),我只能在需要删除该行中所有4个元素(输入)的地方删除它,
JAVASCRIPT
var i = 0; /* Set Global Variable i */
function increment() {
i += 1; /* Function for automatic increment of field's "Name" attribute. */
}
//Function to Remove Form Elements Dynamically
function removeElement(parentDiv, childDiv) {
if (childDiv == parentDiv) {
alert("The parent div cannot be removed.");
} else if (document.getElementById(childDiv)) {
var child = document.getElementById(childDiv);
var parent = document.getElementById(parentDiv);
parent.removeChild(child);
} else {
alert("Child div has already been removed or does not exist.");
return false;
}
}
//Functions that will be called upon, when user click on the Name text field.
function nameFunction() {
var r = document.createElement('span');
var y = document.createElement("INPUT");
y.setAttribute("type", "text");
y.setAttribute("placeholder", "Name");
var g = document.createElement("IMG");
g.setAttribute("src", "img/delete.png");
increment();
y.setAttribute("Name", "quantityordered_" + i);
r.appendChild(y);
g.setAttribute("onclick", "removeElement('myForm','id_" + i + "')");
r.appendChild(g);
r.setAttribute("id", "id_" + i);
document.getElementById("myForm").appendChild(r);
var r = document.createElement('span');
var y = document.createElement("INPUT");
y.setAttribute("type", "text");
y.setAttribute("placeholder", "QTY Delivered");
var g = document.createElement("IMG");
g.setAttribute("src", "img/delete.png");
increment();
y.setAttribute("Name", "quantitydelivered_" + i);
r.appendChild(y);
g.setAttribute("onclick", "removeElement('myForm','id_" + i + "')");
r.appendChild(g);
r.setAttribute("id", "id_" + i);
document.getElementById("myForm").appendChild(r);
var r = document.createElement('span');
var y = document.createElement("INPUT");
y.setAttribute("type", "text");
y.setAttribute("placeholder", "Description");
var g = document.createElement("IMG");
g.setAttribute("src", "img/delete.png");
increment();
y.setAttribute("Name", "description_" + i);
r.appendChild(y);
g.setAttribute("onclick", "removeElement('myForm','id_" + i + "')");
r.appendChild(g);
r.setAttribute("id", "id_" + i);
document.getElementById("myForm").appendChild(r);
var r = document.createElement('span');
var y = document.createElement("INPUT");
y.setAttribute("type", "text");
y.setAttribute("placeholder", "Price ie; £15.00");
var g = document.createElement("IMG");
g.setAttribute("src", "img/delete.png");
increment();
y.setAttribute("Name", "price_" + i);
r.appendChild(y);
g.setAttribute("onclick", "removeElement('myForm','id_" + i + "')");
r.appendChild(g);
r.setAttribute("id", "id_" + i);
document.getElementById("myForm").appendChild(r);
}
//Functions that will be called upon, when user click on the Reset Button.
function resetElements() {
document.getElementById('myForm').innerHTML = '';
}
HTML
<div class="col-md-12" style="margin-top:20px; padding-left:30px;">
<button type="button" onclick="nameFunction()" class="btn btn-info">
<i class="fa fa-plus"></i>
Add another Item
</button>
</div>
我希望这有帮助? 请注意Iam是JS的新手,并认为这将是深入研究etc的绝佳项目。
在此先感谢您提供任何建议
我认为最好的办法是将所有的四个创建的元素放到周围div
,给它一个id
,并通过将其删除id
。
但是,如果要保留当前结构,可以按类名将其删除。
1)创建一个用于从i
生成类名的函数(假设nameFunction
创建4个元素):
function getClassName(i) {
return "name_spans_" + ~~((i - 1)/4);
}
2)在每次调用increment()
之后,在每个生成的跨度中添加一个类:
increment(); // after this line
r.className += getClassName(i);
3)修改onclick
处理程序以传递最后生成的跨度的i
:
g.setAttribute("onclick", "removeElement('myForm'," + i + ")");
4)修改removeElement
函数以删除具有与传递的i
相对应的类名的所有元素:
function removeElement(parentDiv, i) {
var parent = document.getElementById(parentDiv);
var children = [].slice.call(document.getElementsByClassName(getClassName(i)));
for (var c = 0; c < children.length; ++c) {
parent.removeChild(children[c]);
}
}
我的响应类似于@ user3153664一个,但是我将“创建元素”阶段简化为一个函数,并试图清理代码...
var i = 0; /* Set Global Variable i */ function increment() { i += 1; /* Function for automatic increment of field's "Name" attribute. */ } //Function to Remove Form Elements Dynamically function removeElement(parentDiv, childDiv) { if (childDiv == parentDiv) { alert("The parent div cannot be removed."); } else if (document.getElementById(childDiv)) { var child = document.getElementById(childDiv); var parent = document.getElementById(parentDiv); parent.removeChild(child); } else { alert("Child div has already been removed or does not exist."); return false; } } function createInputElement(row, placeholder, name) { var y = document.createElement("INPUT"); y.setAttribute("type", "text"); y.setAttribute("placeholder", placeholder); var g = document.createElement("IMG"); g.setAttribute("src", "img/delete.png"); g.setAttribute("alt", "X"); y.setAttribute("Name", name); var r = document.createElement('span'); r.appendChild(y); g.onclick = function() { removeElement('myForm', row.getAttribute('id')) }; r.appendChild(g); return r; } //Functions that will be called upon, when user click on the Name text field. function nameFunction() { var row = document.createElement('P'); // a set of 4 elements to be delete together increment(); row.setAttribute("id", "id_" + i); // set the row id with the value of i row.appendChild(createInputElement(row, 'Name', 'quantityordered_'+i )); increment(); row.appendChild(createInputElement(row, 'QTY Delivered', 'quantitydelivered_'+i )); increment(); row.appendChild(createInputElement(row, 'Description', 'description_'+i )); increment(); row.appendChild(createInputElement(row, 'Price ie; £15.00', 'price_'+i )); document.getElementById("myForm").appendChild(row); } //Functions that will be called upon, when user click on the Reset Button. function resetElements() { document.getElementById('myForm').innerHTML = ''; }
<form id="myForm"> <div class="col-md-12" style="margin-top:20px; padding-left:30px;"> <button type="button" onclick="nameFunction()" class="btn btn-info"> <i class="fa fa-plus"></i> Add another Item </button> </div> </form>
将所有4个输入插入一个“容器”中-您可以通过删除一个容器将其全部删除。
var i = 0; /* Set Global Variable i */ function increment() { i += 1; /* Function for automatic increment of field's "Name" attribute. */ } //Function to Remove Form Elements Dynamically function removeElement(childId) { // Simplify form - only need a childId if (document.getElementById(childId)) { var child = document.getElementById(childId); child.parentNode.removeChild(child); } } //Functions that will be called upon, when user click on the Name text field. function nameFunction() { // Create a container for all 4 inputs var container = document.createElement('div'); var containerId = 'id_' + i; container.id = containerId; increment(); var r = document.createElement('span'); var y = document.createElement("INPUT"); y.setAttribute("type", "text"); y.setAttribute("placeholder", "Name"); var g = document.createElement("IMG"); g.setAttribute("src", "img/delete.png"); g.setAttribute("onclick", "removeElement('" + containerId + "')"); y.setAttribute("Name", "quantityordered_" + i); r.appendChild(y); r.appendChild(g); container.appendChild(r); increment(); var r = document.createElement('span'); var y = document.createElement("INPUT"); y.setAttribute("type", "text"); y.setAttribute("placeholder", "QTY Delivered"); var g = document.createElement("IMG"); g.setAttribute("src", "img/delete.png"); g.setAttribute("onclick", "removeElement('" + containerId + "')"); y.setAttribute("Name", "quantitydelivered_" + i); r.appendChild(y); r.appendChild(g); container.appendChild(r); increment(); var r = document.createElement('span'); var y = document.createElement("INPUT"); y.setAttribute("type", "text"); y.setAttribute("placeholder", "Description"); var g = document.createElement("IMG"); g.setAttribute("src", "img/delete.png"); g.setAttribute("onclick", "removeElement('" + containerId + "')"); y.setAttribute("Name", "description_" + i); r.appendChild(y); r.appendChild(g); container.appendChild(r); increment(); var r = document.createElement('span'); var y = document.createElement("INPUT"); y.setAttribute("type", "text"); y.setAttribute("placeholder", "Price ie; £15.00"); var g = document.createElement("IMG"); g.setAttribute("src", "img/delete.png"); g.setAttribute("onclick", "removeElement('" + containerId + "')"); y.setAttribute("Name", "price_" + i); r.appendChild(y); r.appendChild(g); container.appendChild(r); increment(); // Append container to form document.getElementById("myForm").appendChild(container); } //Functions that will be called upon, when user click on the Reset Button. function resetElements() { document.getElementById('myForm').innerHTML = ''; }
<div id="myForm" class="col-md-12" style="margin-top:20px; padding-left:30px;"> <button type="button" onclick="nameFunction()" class="btn btn-info"> <i class="fa fa-plus"></i> Add another Item </button> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.