Javascript 使用jquery动态添加或删除表中的行
我的表格结构如下:Javascript 使用jquery动态添加或删除表中的行,javascript,jquery,Javascript,Jquery,我的表格结构如下: <table id="dataTable"> <thead> <tr><th>Name</th> <th>Value</th></tr> </thead> <TR><TD>Scooby Doo</TD><TD&g
<table id="dataTable">
<thead>
<tr><th>Name</th>
<th>Value</th></tr>
</thead>
<TR><TD>Scooby Doo</TD><TD>6</TD><TD><INPUT TYPE="Button" onClick="AddRow()" VALUE="Add Row"></TD></TR>
</table>
名称
价值
史酷比6
单击“添加行”按钮时,我需要将该按钮更改为“删除”按钮,并在第一行插入新行。第一行必须包含与代码中相同的内容。我该怎么做
单击删除按钮时,我必须能够删除删除按钮所属的行。类似于
$('#dataTable thead').prepend('<tr><th>Name</th><th>Value</th></tr>');
.希望这有帮助
$(function(){
$("input[type='button'].AddRow").toggle(
function(){
var el = $(this);
el.closest('tr').clone(true).prependTo(el.closest('table'));
el.attr("value", "Delete row");
},
function(){
$(this).closest('tr').remove();
});
});
<table id="dataTable" border="1">
<thead>
<tr>
<th>
Name
</th>
<th>
Value
</th>
</tr>
</thead>
<tr>
<td>
Scooby Doo
</td>
<td>
6
</td>
<td>
<input type="Button" value="Add Row" class="AddRow">
</td>
</tr>
</table>
$(函数(){
$(“输入[type='button'].AddRow”).toggle(
函数(){
var el=$(本);
el.clone('tr').clone(true).prependTo(el.closest('table'));
el.attr(“值”、“删除行”);
},
函数(){
$(this).closest('tr').remove();
});
});
名称
价值
史酷比
6.
$(function(){
$("input[type='button'].AddRow").toggle(
function(){
var el = $(this);
el.closest('tr').clone(true).prependTo(el.closest('table'));
el.attr("value", "Delete row");
},
function(){
$(this).closest('tr').remove();
});
});
<table id="dataTable" border="1">
<thead>
<tr>
<th>
Name
</th>
<th>
Value
</th>
</tr>
</thead>
<tr>
<td>
Scooby Doo
</td>
<td>
6
</td>
<td>
<input type="Button" value="Add Row" class="AddRow">
</td>
</tr>
</table>