转: js获取table里所有checkbox元素
无标题文档
1、获取所有checkbox里的值
| 1 |
| 2 |
| 3 |
<!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>无标题文档</title>
<script>
function get()
{
var c=document.getElementById("ctable").getElementsByTagName("input");
for(var i=0;i<c.length;i++){
if(c[i].type=="checkbox"){ alert(c[i].value); }
}
}
</script>
</head>
<body>
<table id="ctable">
<tbody>
<tr>
<td>
<input id="c1" type="checkbox" name="c1" value="1" / />1
</td>
</tr>
<tr>
<td>
<input id="c2" type="checkbox" name="c2" value="2" />2
</td>
</tr>
<tr>
<td>
<input id="c3" type="checkbox" name="c3" value="3" />3
</td>
</tr>
</tbody>
</table><p> </p>
<input type="button" onclick="get()" value="提交" />
</body>
</html>
2、获取选中的checkbox的值
<!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>无标题文档</title> <script> function get() { var c=document.getElementById("ctable").getElementsByTagName("input"); for(var i=0;i<c.length;i++){ if(c[i].type=="checkbox"&&c[i].checked){ alert(c[i].value); } } } </script> </head> <body> <table id="ctable"> <tbody> <tr> <td> <input id="c1" type="checkbox" name="c1" value="1" / />1 </td> </tr> <tr> <td> <input id="c2" type="checkbox" name="c2" value="2" />2 </td> </tr> <tr> <td> <input id="c3" type="checkbox" name="c3" value="3" />3 </td> </tr> </tbody> </table><p> </p> <input type="button" onclick="get()" value="提交" /> </body> </html>
浙公网安备 33010602011771号