一些背景信息:
使用for循环创建带有按钮旁边的输入框(用于删除每个按钮的按钮)。 (创建的数量取决于用户在页面重新加载之前离开屏幕的数量)。 显然,删除'由于范围问题,按钮不起作用,因为它们是在循环中创建的。下面的相关代码:
for (var x = 0; x < currentTerms.length; x++){
amt++;
}
//This loop creates the necessarry amount of inputs needed, given the amount of stored
//search terms:
for (var f = 1; f < amt; f++){
var id = idVal++;
count++;
//Create a div to hold the new input box &
//the new "remove" button:
var divv = document.createElement("div");
divv.setAttribute("id", "div"+count);
divv.setAttribute("class", "divvv");
inputContainer.appendChild(divv);
//Create the new input field, set the type, class & id.
var newField = document.createElement("input");
newField.setAttribute('type', 'text');
newField.setAttribute('class', 'field');
newField.setAttribute('id', "field"+id);
divv.appendChild(newField);
//The deleteCont div holds the remove button and it is
//added to the other div, now there is an input field
//and another div inside the div:
var deleteCont = document.createElement("div");
var divId = "cont"+count;
deleteCont.setAttribute("id", id);
deleteCont.setAttribute("class", "remCont");
divv.appendChild(deleteCont);
//Finally, create the remove button itself and add it to the
//container div that was created and place in the initial div:
var remove = document.createElement("input");
remove.setAttribute("type", "button");
remove.setAttribute("value", "-");
remove.setAttribute('class', 'remove');
remove.setAttribute('id', "remove"+id);
deleteCont.appendChild(remove);
//When the user clicks the remove button with "-" value:
remove.onclick = function () {
divv.parentNode.removeChild(divv);
//Remove the error that may have been onscreen:
document.getElementById("max").style.visibility="hidden";
count--;
toggleAddButton("show");
};
}
在用户点击删除按钮时调用的函数中,您可以看到它当然不知道哪个&#39; divv&#39;要删除的元素。
根据我的理解,这可以通过.nameIndex之类的东西轻松解决? 我无法记住它是如何完成的。
任何指针都会有很大的帮助。