您现在的位置是:首页 > 电脑技术查询 > web开发

JS 多个checkbox判断是否选中有关问题

编辑:chaxungu时间:2022-10-02 23:16:17分类:web开发

JS 多个checkbox判断是否选中问题
HTML code<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>
本来想弄个调查表,但是这里不知道怎么判断,每个标题下有好几个选项 ,选项都是多选的 !
麻烦高手指导一下 谢谢 !~~

------解决方案--------------------
var conditions=document.getElementByName("conditions[]");
for(var i=0;i<condition.length;i++){
if(condition[i].checked=true)
; //do something
}

那一个也类似
------解决方案--------------------
if(condition[i].checked==true)试试
------解决方案--------------------
或者if(condition[i].checked=="checked")试试 不知为什么 有时候true可以用 有时候"checked"才能正常工作 不知是不是浏览器的问题 呵呵
------解决方案--------------------
HTML code<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>