[英]Small Javascript Issue with removing multiple elements from dynamic form
Can anyone shed any light on to what I'm doing wrong here if possible? 如果可能的话,任何人都可以阐明我在做什么错吗?
We have a basic form on a php page with javascript that creates 4 form fields when a button is clicked. 我们在php页面上有一个带有javascript的基本表单,当单击按钮时会创建4个表单字段。
If the button is clicked multiple times then it will keep creating rows of 4 columns. 如果多次单击该按钮,它将继续创建4列的行。 The problem is trying to remove the row (4 elements) if someone clicks the button, I can only ever get it to delete one element where I need it to remove all 4 elements (inputs) in that row,
问题是如果有人单击该按钮,则试图删除该行(4个元素),我只能在需要删除该行中所有4个元素(输入)的地方删除它,
JAVASCRIPT 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 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>
I hope this helps? 我希望这有帮助? Please note Iam new to JS and thought this would be a great project to dive into etc.
请注意Iam是JS的新手,并认为这将是深入研究etc的绝佳项目。
Many thanks in advance for any advice anyone can give 在此先感谢您提供任何建议
I think the best way is to place all the four created elements into surrounding div
, give it an id
and the remove it by id
. 我认为最好的办法是将所有的四个创建的元素放到周围
div
,给它一个id
,并通过将其删除id
。
But if you want to preserve the current structure you can remove them by a class name. 但是,如果要保留当前结构,可以按类名将其删除。
1) Create a function for generating class name from i
(assuming that nameFunction
creates 4 elements): 1)创建一个用于从
i
生成类名的函数(假设nameFunction
创建4个元素):
function getClassName(i) {
return "name_spans_" + ~~((i - 1)/4);
}
2) Add a class to each generated span after each call to increment()
: 2)在每次调用
increment()
之后,在每个生成的跨度中添加一个类:
increment(); // after this line
r.className += getClassName(i);
3) Modify onclick
handler to pass i
of the last generated span: 3)修改
onclick
处理程序以传递最后生成的跨度的i
:
g.setAttribute("onclick", "removeElement('myForm'," + i + ")");
4) Modify removeElement
function to remove all elements with a class name corresponding to passed 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]);
}
}
My response is similare to @user3153664 one, but i simplifiend the "create element" phase into one function and tryed to clean the code... 我的响应类似于@ 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>
Insert all 4 inputs into one "container" - and you can remove all of it by removing a container. 将所有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.