读书人

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

发布时间: 2012-03-12 12:45:33 作者: rapoo

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> 


[解决办法]
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和火狐都可以啊

读书人网 >JavaScript

热点推荐