<!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=gb2312" /> <title>无标题文档 </title> </head> <script> 
function f(){ 
for(var i=0;i <3;i++){ 
var tr = document.createElement("tr"); 
var td1=document.createElement("td"); 
var td2=document.createElement("td"); 
var td3=document.createElement("td"); 
var text3 = document.createElement("input"); 
text3.setAttribute("type","text"); 
text3.setAttribute("value","3"); 
var text2 = document.createElement("<input name='re'>"); 
text2.setAttribute("type","text"); 
text2.setAttribute("value","2"); 
var checkbox=document.createElement("input"); 
checkbox.setAttribute("type","checkbox"); 
//checkbox.setAttribute("name","re"); 
checkbox.setAttribute("value",i); 
td1.appendChild(checkbox); 
td2.appendChild(text2); 
td3.appendChild(text3); 
tr.appendChild(td1); 
tr.appendChild(td2); 
tr.appendChild(td3); 
document.getElementById("tb").appendChild(tr); 


function c(){ 
var sa = document.getElementsByName("re");    
alert(sa.length); 

</script> 
<BODY onload="f();"> 
  <table> 
  <tr> 
<td>a </td> 
<td>b </td> 
<td>c </td> 
  </tr> 
  <tbody id="tb"> 
  </tbody>  
  </table> 
  <input type="button" onClick="c();" value="onclick"> 
</BODY>
</html> 

解决方案 »

  1.   

    在给新增项name的是时候如果使用checkbox.setAttribute("name","re"); 就会出现取不到的情况 应改为:var text2 = document.createElement(" <input name='re'>"); 
      

  2.   

    楼上的,你的方法不好使,而且我肯定checkbox得到name值了,我用var ch=document.getElementsByTagName("input");
    alert(ch[0].name);测试了,打印出来“re”了
      

  3.   

    不好使??点击按钮没有得到你要的checkbox个数??
      

  4.   

    不好意思,贴文本多了个空格 贴源码就没问题了<!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=gb2312" /> <title>无标题文档 </title> </head> <script> 
    function f(){ 
    for(var i=0;i <3;i++){ 
    var tr = document.createElement("tr"); 
    var td1=document.createElement("td"); 
    var td2=document.createElement("td"); 
    var td3=document.createElement("td"); 
    var text3 = document.createElement("input"); 
    text3.setAttribute("type","text"); 
    text3.setAttribute("value","3"); 
    var text2 = document.createElement("<input name='re'>"); 
    text2.setAttribute("type","text"); 
    text2.setAttribute("value","2"); 
    var checkbox=document.createElement("input"); 
    checkbox.setAttribute("type","checkbox"); 
    //checkbox.setAttribute("name","re"); 
    checkbox.setAttribute("value",i); 
    td1.appendChild(checkbox); 
    td2.appendChild(text2); 
    td3.appendChild(text3); 
    tr.appendChild(td1); 
    tr.appendChild(td2); 
    tr.appendChild(td3); 
    document.getElementById("tb").appendChild(tr); 


    function c(){ 
    var sa = document.getElementsByName("re");    
    alert(sa.length); 

    </script> 
    <BODY onload="f();"> 
      <table> 
      <tr> 
    <td>a </td> 
    <td>b </td> 
    <td>c </td> 
      </tr> 
      <tbody id="tb"> 
      </tbody>  
      </table> 
      <input type="button" onClick="c();" value="onclick"> 
    </BODY> 
    </html> 
      

  5.   

    var text2 = document.createElement(" <input name='re'>"); <input 前的空格去掉
      

  6.   

    行了,谢了!
    但是我不明白原来的方法,我用var ch=document.getElementsByTagName("input"); alert(ch[0].name);测试了,证实checkbox获得name了,为什么取不到呢