js里重复定义变量是允许的<script type="text/javascript">
function test(){
var form = document.forms["form"];
var myTable = document.getElementById("myTable");
var myTR =myTable.insertRow();     
var myTD=myTR.insertCell();
myTD.innerText = form.txt.value;
var myTD=myTR.insertCell();
myTD.innerHTML= '<input type="button" name="name" value="删除" >';
}
</script>
<form id="form" name="form">
<input type="text" id="txt" name="txt" />
<input type="button" value="Test" onclick="test()" />
  <table width="400" id="myTable">
    <tr>
      <td></td>
    </tr>
  </table>
</form>

解决方案 »

  1.   

    再给个以前写得例子,希望能对你有些帮助:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>DOM动态增删</title>
    <script type="text/javascript">
    /*
    * Author: csser.com | popcg.com
    * mail: [email protected]
    */
    var count = 0;
    function Add(){
    count += 1;
    var File1 = document.getElementById("file1");
    var div = document.createElement("div");
    var countTxt = document.createTextNode("文本框"+count);
    var inputTxt = document.createElement("input");
    inputTxt.type = "text";
    inputTxt.name = "txt"+count;
    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "取消";
    btn.onclick = function(){
    this.parentNode.parentNode.removeChild(this.parentNode);
    var str = File1.innerHTML;
    var re = /[^<]+/i;
    var n = File1.getElementsByTagName("div");
    for(var k=0;k<n.length;k++){
    n[k].innerHTML = n[k].innerHTML.replace(re,"文本框"+(k+1));
    n[k].getElementsByTagName("input").item(1).onclick = this.onclick;
    }
    count -= 1;
    }
    div.appendChild(countTxt);
    div.appendChild(inputTxt);
    div.appendChild(btn);
    File1.appendChild(div);
    }
    </script>
    </head>
    <body><input type="button" name="button" value="增加" onclick="Add();" />
    <div id="file1"></div>
    </body>
    </html>