我在类型"复选框"的表格中有多个输入标签。我需要删除所选复选框的完整行。 我使用以下函数删除选定的行:
function removeSelected( tblname )
{
var tbl = document.getElementById(tblname);
var rowcount = tbl.rows.length;
if (tbl.rows.length > 1)
{
for ( var i = 0 ; i < tbl.rows.length ; i++)
{
var row = tbl.rows[i];
var chkselect = row.cells[0].getElementByTagName("input").item(0);
var attr = trimString(row.cells[0].getElementByTagName("input").item(1).value);
var attr1 = trimString(row.cells[0].getElementByTagName("input").item(2).value);
if (chkselect.checked)
{
tbl.deleteRow(i);
i = 0;
}
}
}
}
我的HTML如下:
<table id="myTable" border = "0">
<tr border = "0">
<td><input type="checkbox" name="checkbox" id = "checkbox" value = "hen">hen</input></td>
</tr>
<tr>
<td><input type = "checkbox" name="checkbox1" id="checkbox1" value = "cock">cock</input></td>
</tr>
</table>
<br>
<button onclick="removeSelected("myTable")">Try it</button>
当我检查&#34;母鸡&#34;复选框它应该删除母鸡行,当我检查&#34; cock&#34;它应该删除公鸡排。但是,我无法删除所选的一个。
我做错了什么?
答案 0 :(得分:0)
正如AleJuliet
所说,你应该修复语法错误。
要删除行,您应该从最后开始,因为行已移位
function removeSelected( tblname ) {
var tbl = document.getElementById(tblname);
for ( var i = tbl.rows.length - 1 ; i >= 0 ; i--) {
checkbox = tbl.rows[i].getElementsByTagName("input")[0];
if(checkbox.checked){
tbl.deleteRow(i);
}
}
}
&#13;
<table id="myTable" border = "0">
<tr border = "0">
<td><input type="checkbox" name="checkbox" id = "checkbox" value = "hen">hen</input></td>
</tr>
<tr>
<td><input type = "checkbox" name="checkbox1" id="checkbox1" value = "cock">cock</input></td>
</tr>
</table>
<br>
<button onclick="removeSelected('myTable')">Try it</button>
&#13;