我在字段集中添加测试,虽然我可以添加它们,但我不确定如何编写正确的函数来删除它们。我曾在JavaScript中工作,但被要求使用JQuery编写它,似乎无法使其工作。我研究过的所有例子似乎都不适用于我的原始克隆函数,它在其中构建了一个删除按钮。字段集也是重复的,我已经有了代码工作,只需要一点帮助就可以使用这个删除事件函数。jQuery删除编号
这的JavaScript/jquery的:
document.getElementById('button').onclick = duplicate;
var i = 0;
var original = document.getElementById('dataTes');
function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "dataTes" + ++i; // there can only be one element with an ID
original.parentNode.appendChild(clone);
}
function remove(){
}
下面是HTML:ⅰ的
<fieldset>
<legend>Test</legend>
<input type="submit" class="button" id = "button" value="+" onlick="duplicate()" title = "#">
<input type="submit" class="button" id = "button" value="-" onlick="remove()" title = "#">
<div id="dataTes">
<table align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse;margin-top:10px;" width="97%">
<tr>
<td width="100px">Test</td>
<td width="2px">:</td>
<td width="2px"><input type="text" name="usrname"></td>
<td>
<input type="submit" class="button" id = "add" value="+" onClick="addRow('#')" title = "#">
<input type="submit" class="button" id = "add" value="-" onClick="deleteRow('#')" title = "#">
</td>
</tr>
<table align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse;margin-top:5px;margin-left:40px;" width="97%">
<tr>
<td width="2px"></td>
<td width="100px">Fill</td>
<td width="2px">:</td>
<td><input type="text" name="usrname"></td>
</tr>
<tr>
<td width="2px"></td>
<td width="100px">Fill</td>
<td width="2px">:</td>
<td><input type="text" name="usrname"></td>
</tr>
<table id="dataID" align="center" cellpadding="3" cellspacing="0" style="border-collapse:collapse;margin-left:40px;" width="97%">
<tr>
<td width="2px"></td>
<td width="100px">Fill</td>
<td width="2px">:</td>
<td>
<input type="text" name="usrname">
</td>
</tr>
</table>
</table>
</table>
</div>
</fieldset>
您是否尝试设置知名度? – Li357
是的,我试过@AndrewL。 – Exodius35
问题1:这可以在没有id的'dataTes'的情况下完成,是否有使用Id的特定原因?问题2:你是否意识到你的HTML非常无效? 'table'不是'table'的有效子元素。如果你在一张表中使用一张表(这通常是一种不好的做法),它需要在'td'中,这当然需要在'tr'中。 –