<!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>
<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>
alert(ch[0].name);测试了,打印出来“re”了
<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>
但是我不明白原来的方法,我用var ch=document.getElementsByTagName("input"); alert(ch[0].name);测试了,证实checkbox获得name了,为什么取不到呢