JS 多个checkbox判断是否选中问题
<form action="" method="post" name="voteform" id="voteform" onsubmit="return Check();"><table> <tr> <td width="75"></td> <td colspan="4" class="votetitle" height="18">A</td> <tr> <tr> <td width="75"></td> <td width="140"><input type="checkbox" name="conditions[]" value="1" />1</td> <td width="140"><input type="checkbox" name="conditions[]" value="2" />2</td> <td width="140"><input type="checkbox" name="conditions[]" value="3" />3</td> <td width="140"><input type="checkbox" name="conditions[]" value="4" />4</td> </tr> <tr> <td width="75"></td> <td colspan="4" class="votetitle" height="18">B</td> <tr> <tr> <td width="75"></td> <td width="140"><input type="checkbox" name="hstyle[]" value="1" />1</td> <td width="140"><input type="checkbox" name="hstyle[]" value="2" />2</td> <td width="140"><input type="checkbox" name="hstyle[]" value="3" />3</td> <td width="140"><input type="checkbox" name="hstyle[]" value="4" />4</td> </tr> <tr><td colspan="5" height="15"></td></tr> <tr> <td colspan="5" class="votetitle" height="18" align="center"><input type="submit" name="submit" value="提交"/></td> <tr> </table></form>
<html><head><script>function Check(form){ var obj = form.elements["conditions[]"]; var arr = []; arr.push("conditions[]: "); for(var i = 0; i < obj.length; i++){ if(obj[i].checked){ arr.push(obj[i].value); } } var obj1 = form.elements["hstyle[]"]; arr.push(" hstyle[]: "); for(var i = 0; i < obj1.length; i++){ if(obj1[i].checked){ arr.push(obj1[i].value); } } alert(arr.join(",")); return false;}</script></head><body><form action="" method="post" name="voteform" id="voteform" onsubmit="return Check(this);"><table> <tr> <td width="75"></td> <td colspan="4" class="votetitle" height="18">A</td> <tr> <tr> <td width="75"></td> <td width="140"><input type="checkbox" name="conditions[]" value="1" />1</td> <td width="140"><input type="checkbox" name="conditions[]" value="2" />2</td> <td width="140"><input type="checkbox" name="conditions[]" value="3" />3</td> <td width="140"><input type="checkbox" name="conditions[]" value="4" />4</td> </tr> <tr> <td width="75"></td> <td colspan="4" class="votetitle" height="18">B</td> <tr> <tr> <td width="75"></td> <td width="140"><input type="checkbox" name="hstyle[]" value="1" />1</td> <td width="140"><input type="checkbox" name="hstyle[]" value="2" />2</td> <td width="140"><input type="checkbox" name="hstyle[]" value="3" />3</td> <td width="140"><input type="checkbox" name="hstyle[]" value="4" />4</td> </tr> <tr><td colspan="5" height="15"></td></tr> <tr> <td colspan="5" class="votetitle" height="18" align="center"><input type="submit" name="submit" value="提交"/></td> <tr> </table></form></body></html>
[解决办法]
var cond=document.getElementsByName("conditions[]");
firebug 可以调试js 的
[解决办法]
<script type="text/javascript">
function ch(){
var a=document.getElementsByName("s");
for(var i=0;i<a.length;i++){
if(a[i].checked==true||a[i].checked=="checked"){
alert(i);
}
}
}
</script>
</head>
<body>
<input type="checkbox" name="s">
<input type="checkbox" name="s">
<input type="checkbox" name="s">
<input type="checkbox" name="s">
<input type="checkbox" name="s">
<input type="checkbox" name="s">
<input type="button" id="test" onclick="ch()">
</body>
这样写了一个 ie和火狐都可以啊