如何使用添加和删除按钮动态添加表以及动态添加行
How to dynamically add tables along with dynamic addition of rows using add and delete buttons for both?
我的JAVASCRIPT代码
var a1=0;
var count=1;
function addRow(data)
{
var table = document.getElementById('student');
var rowCount = table.rows.length;
var col = table.rows[2].cells.length;
var del = (data.id);
var row = table.insertRow(rowCount);
for(i=0;i<col;i++)
{
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[2].cells[i].innerHTML;
}
}
function addst(data)
{
var table = document.getElementById('student');
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
row.innerHTML = ' ';
for(i=0;i<3;i++)
{
var row = table.insertRow(rowCount+i+1);
row.innerHTML = table.rows[i].innerHTML;
if(i==0)
{
row.id="st"+a1+"";
}
}
count++;
}
function delRow(data)
{
var table = document.getElementById('student');
var rowCount = table.rows.length;
var p=data.parentNode.parentNode;
p.parentNode.removeChild(p);
}
function delst(data)
{
var table = document.getElementById('student');
var rowCount = table.rows.length;
var p = document.getElementById(eq01);
table.removeChild(p);
}
我的HTML代码
<fieldset>
<legend align="center">Student Information</legend>
<br>
<table id="student">
<tr id="s1">
<th><select name="" width="250px">
<option value="" selected align="center">student Type</option>
<option value="">male</option>
<option value="">female</option>
</select></th>
<th><input type="button" value="+" onclick="addst(this)"/>
<th><input type="button" value="-" onclick="delst(this)"/>
</tr>
<tr id="label">
<th align="left">class info</th>
<th align="left">Math</th>
<th align="left">Phy</th>
<th align="left">Chem</th>
<th align="left">Total</th>
<th align="left">Average</th>
</tr>
<tr id="c1">
<td>
<select>
<option value="" selected>Select your class</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
</select>
</td>
<td><input type="text" id="val02" size="16" name="math" placeholder="Math marks"/></td>
<td><input type="text" id="val03" size="16" name="phy" placeholder="Phy marks"/></td>
<td><input type="text" id="val04" size="16" name="chem" placeholder="Chem marks"/></td>
<td><input type="text" id="val06" size="16" name="total" placeholder="Total marks"/></td>
<td><input type="text" id="val07" size="16" name="avg" placeholder="Average marks"/></td>
<th><input type="button" value="+" id="r1" onclick="addRow(this)" /></th>
<th><input type="button" value="-" id="r1" onclick="delRow(this)" /></th>
</tr>
</TABLE>
</fieldset>
'addst'和'addRow'函数正在工作,但不知道如何编写删除函数。。。。你们能帮我搞定吗
'delst'应删除所有相应的学生信息,而'delRow'则应删除每个单独的行
您已经编写了一个名为DeleteRow的函数,但您在click事件中调用了delRow(this)。正因为如此,你才会犯错。
像这样尝试
function delst(data)
{
var table = document.getElementById('student');
var rowCount = table.rows.length;
var p=data.parentNode.parentNode;
p.parentNode.removeChild(p);
}
请将每个数据绑定到单独的表中,然后尝试删除行下的表。删除父子元素将很容易。示例:
<table>
<tr>
<td>
<table><tr><!--your row data--><td></td></tr></table>
</td>
</tr>
</table>
相关文章:
- 如何在Google柱状图中动态添加行/列
- 使用jQuery动态添加表并在其中动态添加行
- 如何使用Angular动态添加iframe-src
- 如何向JSON数组动态添加属性
- 动态添加通过json创建的选择项
- 使用jQuery根据动态生成的html动态添加html
- 使用.on动态添加jquery/js不知道的html元素
- JS动态添加字段-删除按钮不起作用
- 动态添加AngularJS脚本
- 将表行动态添加到表或tbody元素中
- jQuery-通过AJAX调用动态添加具有值的表单元格
- 将特定属性动态添加到元素中
- 在highcharts.js中向点弹出窗口动态添加文本
- 如何在Angular.js中循环动态添加Fields并获取数据并将其发送到服务器
- 使用jQuery动态添加onClick事件
- 如何在Opencart中动态添加JavaScript
- 剑道UI下载列表:如何动态添加新元素
- 动态添加复选框和输入框 jQuery
- 动态添加哈希标记;t工作
- 将图表动态添加到组合高图表中