JavaScript动态添加和删除行

//添加按钮定义中的按钮

function addBtnRow() {

var table = document.getElementById('btnList');

var rownum = table.rows.length;

var newrow = table.insertRow(rownum);

var newcell = newrow.insertCell();

newcell.innerHTML = "<input name='checkbox' type='radio' value='1'><input name='flags' type='hidden' value='1' ><input name='confIds' value='' type='hidden' />&nbsp;"+rownum;

newcell = newrow.insertCell();

newcell.innerHTML = "<input name='bvalue' value='' type='text' style='width:100%' >";

newcell = newrow.insertCell();

newcell.innerHTML = "<input name='bevent' value='' type='text' style='width:500px' ondblclick=openWin(this,'395','200'); >";

newrow.cells[1].children[0].focus();

}

//删除按钮定义中的按钮

function delBtnRow(){

var table = document.getElementById("btnList");

for(var i=1; i<table.rows.length; i++){

if(table.rows[i].cells[0].children[0].checked){

if(table.rows[i].cells[0].children[2].value!=""){

document.forms[0].delConfId.value+=table.rows[i].cells[0].children[2].value+"@";

}

table.deleteRow(i);

}

}

for(var i=1; i<table.rows.length; i++){//删除某行后,重新调整序号值

table.rows[i].cells[0].innerHTML = "<input name='checkbox1' type='radio' value='1'>&nbsp;"+i;

}

}

应用举例:

<table id="btnList" width="100%" class="tabDef">

<tr>

<td nowrap align="center" width="10%">序号</td>

<td nowrap align="center" >名称</td>

<td nowrap align="center" width="500">方法或脚本</td>

</tr>

</table>

你可能感兴趣的:(JavaScript,脚本)