<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>本来想弄个调查表,但是这里不知道怎么判断,每个标题下有好几个选项 ,选项都是多选的 !
麻烦高手指导一下 谢谢 !~~
for(var i=0;i<condition.length;i++){
if(condition[i].checked=true)
; //do something
}那一个也类似
测试了 没反应 。。<script>
function Check(){
var cond=document.getElementByName("conditions[]");
for(var i=0;i<cond.length;i++){
if(conditions[i].checked=true)
alert("吓死爹啊");
}
}
</script>是这个样子吗??
<table> <tr>
<td width="75"></td>
<td colspan="4" class="votetitle" height="18">NAME:<input type="text" name="name" /></td>
<tr>
<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>
<script>
function Check(){if (document.voteform.name.value == ""){
alert("想吓死爹啊 ");
document.voteform.name.focus();
return false;
}
var cond=document.getElementByName("conditions[]");
for(var i=0;i<cond.length;i++){
if(conditions[i].checked == "checked" || conditions[i].checked == true)
alert("吓死爹啊");
}
}
</script>这是我测试的 我加了一个 name 测试一下可以 就是 checkbox 没什么反应 ,你们可以试试 ,我用的firefox !~ 谢谢了
<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>
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和火狐都可以啊
现在总结一下
也为以后小菜做点贡献 !
第一 :2楼var = document.getElementByName("conditions[]"); 中 getElementByName少了一个s 而且 在 for 循环中 condition.length 以及 condition[i].checked=true;中都少了一个s并少了一个等号;而且所以导致楼主 代码一直没通过测试!
所以 2楼的最终代码如下已经测试通过 <script>
function Check(){
var num = document.getElementsByName("conditions[]") ; for(var i=0;i<num.length;i++){
if(num[i].checked == true)
alert("吓死爹啊");
}
}
</script>第二 楼主之前写的 document.voteform.getElementsByName("conditions[]") 不行;
看了8楼大婶的 随改成document.voteform.elements.["conditions[]"]通过了;
所以第二种是这样: <script>
function Check(){
var num = document.voteform.elements.["conditions[]"]; for(var i=0;i<num.length;i++){
if(num[i].checked == true)
alert("吓死爹啊");
}
}
</script>当然 8楼 大婶完全可以使用 谢谢 8楼指点迷津啊!~~