[英]How to delete table row using Javascript
I have created new rows using Javascript function addRow(id)
, but I can't delete it. 我已经使用Javascript function addRow(id)
创建了新行,但是无法删除它。 When I click the remove button, nothing happens. 当我单击删除按钮时,什么也没有发生。 What am I doing wrong? 我究竟做错了什么? I'm grateful for any advice. 我很感谢您的任何建议。
<table>
<tr>
somethingElse
</tr>
<tr>
<td colspan="5">
<script type="text/javascript">
var inputCount = 0;
function addRow(id) {
//set row number
inputCount++;
var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
var row = document.createElement("TR");
var td1 = document.createElement("TD");
var td2 = document.createElement("TD");
var td3 = document.createElement("TD");
var td4 = document.createElement("TD");
var td5 = document.createElement("TD");
var t1 = document.createElement('div');
var t2 = document.createElement('div');
var t3 = document.createElement('div');
var t4 = document.createElement('div');
var t5 = document.createElement('div');
t1.innerHTML = "Name of school: ";
t2.innerHTML = "<input type='text' Width='180px' name='items_" + inputCount + "'>";
t3.innerHTML = "Title aquired: ";
t4.innerHTML = "<input type='text' Width='180px' name='value_" + inputCount + "'>";
t5.innerHTML = "<input type='Button' class='Button' onclick='deleteLine(this)' value='Remove'>";
td1.appendChild(t1)
td2.appendChild(t2)
td3.appendChild(t3)
td4.appendChild(t4)
td5.appendChild(t5)
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
row.appendChild(td5);
tbody.appendChild(row);
}
function deleteLine(object) {
var table = document.getElementsByTagName("myTable")[0];
var tBody = table.getElementsByTagName("tbody")[0];
var rows = tBody.getElementsByTagName("tr");
while (object.tagName != 'TR') {
object = object.parentNode;
}
var row = rows[object.rowIndex];
tBody.removeChild(row);
}
</script>
<table id="myTable">
<tr>
<td align=right>
Name of school:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="180px"></asp:TextBox>
</td>
<td align=right>
Title aquired:
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="180px"></asp:TextBox>
</td>
<td>
<a href="javascript:addRow('myTable')">Add more education</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
somethingElse
</tr>
</table>
I guess this 我猜这个
var table = document.getElementsByTagName("myTable")[0];
should better be 最好是
var table = document.getElementById("myTable");
the function can be made simpler if you want to: 如果需要,可以简化该功能:
function deleteLine(object) {
while (object.tagName != 'TR') {
object = object.parentNode;
}
object.parentNode.removeChild(object);
}
You can also try this. 您也可以尝试一下。 ... ...
<script type="text/javascript">
var inputCount = 0;
function addRow(id) {
//set row number
inputCount++;
var tbody = document.getElementById(id);
html=""
var row = document.createElement("TR");
row.setAttribute("id", "row"+inputCount)
tbody.appendChild(row);
html +="<td>Name of school:</td>"
html +="<td><input type='text' Width='180px' name='items_" + inputCount + "'></td>"
html +="<td><input type='text' Width='180px' name='items_" + inputCount + "'></td>"
html +="<td>Title aquired: </td>"
html +="<td><input type='Button' class='Button' onclick=deleteLine('row"+inputCount+"') value='Remove'></td>"
alert(html)
row.innerHTML = html
}
function deleteLine(index) {
table = document.getElementById("myTable")
row = document.getElementById(index)
table.removeChild(row);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.