我的代码可以动态添加行 但是当删除当前行后 索引发生了变化 
  例如 当前行我的控件的ID是txtContent2 当删除后该行后 索引txtContent2被删除  txtContext3到了txtContent2的位置
  所以就变成了 txtContent1 txtContent3...
  而不是按顺序输出 txtContent1 txtContent2...
  我在程序中重新让他们按顺序输出 但是不起作用 大家帮忙看一下。  <script language="javascript">// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{    theDoc = parent.frames[theObj.substring(p+1)].document;    theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++)     foundObj = theDoc.forms[theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)     foundObj = findObj(theObj,theDoc.layers.document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);    return foundObj;
}
//添加一个参与人写好行
function AddSignRow(){ //读取最后一行的行号,存放在txtTRLastIndex文本框中
var txtTRLastIndex = findObj("txtTRLastIndex",document);
var rowID = parseInt(txtTRLastIndex.value);
var signFrame = findObj("SignFrame",document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "SignItem" + rowID;
//添加列:序号
var newNameTD=newTR.insertCell(0);
//添加列内容
newNameTD.innerHTML = newTR.rowIndex.toString();
//添加列:工作内容及完成情况
var newNameTD=newTR.insertCell(1);
//添加列内容
newNameTD.innerHTML = "<textarea name='txtContent" + rowID + "' id='txtContent" + rowID + "' rows='4' cols='40' />";
//添加列:成果
var newEmailTD=newTR.insertCell(2);
//添加列内容
newEmailTD.innerHTML = "<textarea name='txtResult" + rowID + "' id='txtResult" + rowID + "' rows='4' cols='40' />";
//添加列:删除按钮
var newDeleteTD=newTR.insertCell(3);
//添加列内容
newDeleteTD.innerHTML = "<div align='center' style='width:40px'><a href='javascript:;' onclick=\"DeleteSignRow('SignItem" + rowID + "')\">删除</a></div>";
//将行号推进下一行
txtTRLastIndex.value = (rowID + 1).toString() ;
}
//删除指定行
function DeleteSignRow(rowid){
var signFrame = findObj("SignFrame",document);
var signItem = findObj(rowid,document);
//获取将要删除的行的Index
var rowIndex = signItem.rowIndex;
//删除指定Index的行
signFrame.deleteRow(rowIndex);
//重新排列序号,如果没有序号,这一步省略
for(i=rowIndex;i<signFrame.rows.length;i++){//将所有input的编号进行重新分配
//要将所有的列都写上
//var old_id=signFrame.rows[i].cells[1].childNodes[0].id;//name
//7为列名txtName的长度
//var new_id=old_id.substr(0,10) + i.toString();
//signFrame.rows[i].cells[1].childNodes[0].id=new_id;
//alert(signFrame.rows[i].cells[1].innerHTML);
//signFrame.rows[i].cells[1].childNodes[0].value=new_id;
//document.getElementById(new_id).value=new_id;
//================================================
signFrame.rows[i].cells[0].innerHTML = i.toString();
}
//重新绑定  但不起作用 为什么呢????
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
for(i=1;i <= rowscount; i++){
  var new_id=old_id.substr(0,10) + i.toString();
  signFrame.rows[i].cells[1].childNodes[0].id=new_id;
}
}
//清空列表
function ClearAllSign(){
if(confirm('确定要清空所有列表内容吗?')){
var signFrame = findObj("SignFrame",document);
var rowscount = signFrame.rows.length;
//连续循环删除行,从最后一行往前删除
for(i=rowscount - 1;i > 0; i--){
   signFrame.deleteRow(i);
}
//重置最后行号为1
var txtTRLastIndex = findObj("txtTRLastIndex",document);
txtTRLastIndex.value = "1";
//预添加一行
AddSignRow();
}
}<div>
<table  border="1" cellpadding="0" cellspacing="0" id="SignFrame">
              <tr id="trHeader">
                <td>序号</td>
                <td>工作内容及完成情况</td>
                <td>成果</td>
                <td align="center">&nbsp; </td>
              </tr>
        </table>
   </div>
   <div>
        <input type="button" name="Submit" value="添加" onclick="AddSignRow()" />
     <input type="button" name="Submit2" value="清空" onclick="ClearAllSign()" />
  <input name='txtTRLastIndex' type='hidden' id='txtTRLastIndex' value="1" />
   </div>

解决方案 »

  1.   

    这是 删除时的代码 我加了重新生成索引 但不起作用 为什么呢?
    //删除指定行
    function DeleteSignRow(rowid){
    var signFrame = findObj("SignFrame",document);
    var signItem = findObj(rowid,document);
    //获取将要删除的行的Index
    var rowIndex = signItem.rowIndex;
    //删除指定Index的行
    signFrame.deleteRow(rowIndex);
    //重新排列序号,如果没有序号,这一步省略
    for(i=rowIndex;i<signFrame.rows.length;i++){//将所有input的编号进行重新分配
    //要将所有的列都写上
    //var old_id=signFrame.rows[i].cells[1].childNodes[0].id;//name
    //7为列名txtName的长度
    //var new_id=old_id.substr(0,10) + i.toString();
    //signFrame.rows[i].cells[1].childNodes[0].id=new_id;
    //alert(signFrame.rows[i].cells[1].innerHTML);
    //signFrame.rows[i].cells[1].childNodes[0].value=new_id;
    //document.getElementById(new_id).value=new_id;
    //================================================
    signFrame.rows[i].cells[0].innerHTML = i.toString();
    }
    ------------------------------------------------------
    这里是重新生成 但没起作用 我从后台取不到
    var signFrame = findObj("SignFrame",document);
    var rowscount = signFrame.rows.length;
    for(i=1;i <= rowscount; i++){
     //var old_id=signFrame.rows[i].cells[1].childNodes[0].id;
      var new_id='txtContent' + i.toString();
      signFrame.rows[i].cells[1].childNodes[0].id=new_id;
    }
    ---------------------------------------------------------
    }
      

  2.   

    看不大明白你要干什么。。
    不过这边的索引是不是该从0开始呢
    signFrame.rows[i].cells[1].childNodes[0].id=new_id;
      

  3.   

    就是索引变化 当你一删除当前行假设当前行第1列中的txtContentID是txtContentID2 当你一删除 它就不存在了 我要的效果是下一行第一列中txtContentID3 顶替txtContentID2 变成 txtContentID2 按顺序输出txtContentID1 txtContentID2..
    而不是txtContentID1 txtContentID3.
      

  4.   

    你在删除的时候可以暂时把当前行的style的display属性修改成none值,让他不要重建索引不就行了.
      

  5.   

    这个删除增加表格我也做过,也是用动态创建,但是这种方法写完感觉不太好,你可以给整个DIV从新初始化。div.innerHTML=<table><tr id="动态循环加"><td id=""></td></tr>