使用 JavaScript 添加和删除(特定)表单元素
我有一个文本区域,旁边有两个按钮:“添加另一个文本区域”和“删除此文本区域”
这是我的html代码:
<div id="summaryIn">
<label for="">Summary of Experience:</label>
<textarea class="TA1" name="mySummary[]" value=""></textarea>
<button class="adddel" name="cmdDelSummary[]" id="cmdDelSummary" title="Delete this Summary" onClick="DelSummary('summaryIn');">-</button>
<button class="adddel" id="cmdAddSummary" title="Add Another Summary" onClick="AddSummary('summaryIn');">+</button>
</div>
我用于添加文本区域的javascript函数工作正常,但是删除按钮不能很好地工作,这里是javascript 代码:
function DelSummary(divName){
alert(summary_counter);
if (summary_counter == 1)
{
document.getElementById('cmdDelSummary').disabled=true;
}
else
{
summaryIn.removeChild(summaryIn.lastChild);
summary_counter--;
//alert(summary_counter);
}
}
我已经尝试解决这个问题很长时间了,但直到现在我还没有找到任何解决方案。 。 .我不知道如何索引/引用附加删除按钮,以便removeChild仅删除该特定文本区域,而不总是删除最后一个Child..提前非常感谢:)
i have a textarea and beside it are two buttons: "add another textarea" and "delete this textarea"
here is my html code:
<div id="summaryIn">
<label for="">Summary of Experience:</label>
<textarea class="TA1" name="mySummary[]" value=""></textarea>
<button class="adddel" name="cmdDelSummary[]" id="cmdDelSummary" title="Delete this Summary" onClick="DelSummary('summaryIn');">-</button>
<button class="adddel" id="cmdAddSummary" title="Add Another Summary" onClick="AddSummary('summaryIn');">+</button>
</div>
my javascript function for adding the textarea works fine, however the delete button doesn't work well, here is the javascript code:
function DelSummary(divName){
alert(summary_counter);
if (summary_counter == 1)
{
document.getElementById('cmdDelSummary').disabled=true;
}
else
{
summaryIn.removeChild(summaryIn.lastChild);
summary_counter--;
//alert(summary_counter);
}
}
i've been trying to figure this out for quite a long time now, but until now i haven't found any solution. . .i don't know how to index/reference the additional delete buttons so that the removeChild would delete that particular textarea only and not always the lastChild..thanks very much in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须
在之前添加
I think you have to add
before
这行:
似乎依赖于将元素名称和 id 放入引用该元素的全局变量的实践。这是一种非标准做法,尚未被所有浏览器实现。
而是使用符合标准的:
现在您可以调用其 removeChild 方法。您可能还应该对要删除的内容进行一些检查,因为最初“添加另一个摘要”按钮是最后一个子元素。
编辑
按照惯例,以大写字母开头的函数名称是为构造函数保留的,所以我使用了小写字母。
要从文档中删除 div 及其内容:
并像这样调用它:
虽然 HTML 属性名称不区分大小写,但我发现使用驼峰式大小写会使它们难以阅读,因此使用小写。
This line:
seems to be dependant on the practice of making element names and ids into global variables that reference the element. That is a non-standard practice that has not been implemented by all browsers.
Instead use the standards-compliant:
Now you can call its removeChild method. You should probaby do some checking of what you are about to remove too, since initially the "Add Another Summary" button is the last element child.
Edit
By convention, function names starting with a capital letter are reserved for cosntructors, so I've used lower case.
To remove the div and its content from the document:
And call it like this:
While HTML attribute names are not case sensitive, I find that using camel case makes them hard to read and so use lower case.