我有父子关系的表。如果删除父亲,则删除其下的儿子。您还可以删除单个儿子。到目前为止,我的代码并没有删除具有相应 id 的父亲或儿子,而是删除了表中的第一件事。 这是我的代码:
<script>
function delVesselAll(num){
$("#vessel_tab #father" + num).remove();
$("#vessel_tab .son" + num).remove();
document.getElementById(id).style.display = 'block';
};
function delSon(num){
$("#vessel_tab #son" + num).remove();
document.getElementById(id).style.display = 'block';
};
</script>
还有我的 HTML:
<table id="vessel_tab">
<tr id="father1">
<td colspan="2" class="father_header">Father 1</td>
<td><a style="text-decoration:none;" onclick="showModal('delAllModal')"><input type="button" value="delete" onclick="showModal('delAllModal')"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:10px; display:none" id="delAllModal">
Are you sure you want to delete the father and corresponding sons?<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delAllModal')"/><input type="button" value="Delete" onclick="delVesselAll(1)" id="delete"/></div>
</tr>
<tr class="son1" id="son1">
<td> </td>
<td>Son 1</td>
<td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(1)"/></div>
</tr>
<tr class="son2" id="son2">
<td> </td>
<td>Son 2</td>
<td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(2)"/></div>
</tr>
<tr id="father2">
<td colspan="2">Father 2</td>
<td><a style="text-decoration:none;" onclick="showModal('delAllModal')"><input type="button" value="delete"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:10px; display:none" id="delAllModal">
Are you sure you want to delete the father and corresponding sons?<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delAllModal')"/><input type="button" value="Delete" onclick="delVesselAll(2)" id="delete"/></div>
</tr>
<tr class="son3" id="son3">
<td> </td>
<td>Son 3</td>
<td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(3)"/></div>
</tr>
<tr class="son4" id="son4">
<td> </td>
<td>Son 4</td>
<td><a style="text-decoration:none;" onclick="showModal('delSonModal')"><input type="button" value="delete"></a></td>
<div class="delModal" style="z-index:999999; margin-left:200px; margin-top:30px; display:none" id="delSonModal">
Please confirm deletion.<br /><input type="button" value="Cancel" class="hide" onclick="hideModal('delSonModal')"/><input type="button" value="Delete" onclick="delSon(4)"/></div>
</tr>
</table>
另外,jsfiddle。它工作不正常(不知道我做错了什么),但我的代码在那里。提前致谢! http://jsfiddle.net/7tFFS/1/