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

忧伤的蜗牛 2012-02-24 06:25:54

<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>

本来想弄个调查表,但是这里不知道怎么判断,每个标题下有好几个选项 ,选项都是多选的 !
麻烦高手指导一下 谢谢 !~~
...全文
9968 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
忧伤的蜗牛 2012-02-25
  • 打赏
  • 举报
回复
经过各位的帮助 和 楼主的坚持不懈的钻研 这个问题已经基本搞定 而且已经得知在8楼出现之前为什么各位大神的代码不起作用的原因了
现在总结一下
也为以后小菜做点贡献 !
第一 :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楼指点迷津啊!~~

似梦飞花 2012-02-25
  • 打赏
  • 举报
回复
<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和火狐都可以啊
hch126163 2012-02-25
  • 打赏
  • 举报
回复
var cond=document.getElementsByName("conditions[]");

firebug 可以调试js 的
xie_yanke 2012-02-25
  • 打赏
  • 举报
回复
<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>

忧伤的蜗牛 2012-02-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zzgzzg00 的回复:]

或者if(condition[i].checked=="checked")试试 不知为什么 有时候true可以用 有时候"checked"才能正常工作 不知是不是浏览器的问题 呵呵
[/Quote]
<form action="" method="post" name="voteform" id="voteform" onsubmit="return Check();">
<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 !~ 谢谢了
我爱荤菜 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zzgzzg00 的回复:]
或者if(condition[i].checked=="checked")试试 不知为什么 有时候true可以用 有时候"checked"才能正常工作 不知是不是浏览器的问题 呵呵
[/Quote]对头
色拉油 2012-02-24
  • 打赏
  • 举报
回复
确实少写了个等号
似梦飞花 2012-02-24
  • 打赏
  • 举报
回复
或者if(condition[i].checked=="checked")试试 不知为什么 有时候true可以用 有时候"checked"才能正常工作 不知是不是浏览器的问题 呵呵
似梦飞花 2012-02-24
  • 打赏
  • 举报
回复
if(condition[i].checked==true)试试
忧伤的蜗牛 2012-02-24
  • 打赏
  • 举报
回复
不行啊
测试了 没反应 。。

<script>
function Check(){
var cond=document.getElementByName("conditions[]");
for(var i=0;i<cond.length;i++){
if(conditions[i].checked=true)
alert("吓死爹啊");
}
}
</script>

是这个样子吗??
色拉油 2012-02-24
  • 打赏
  • 举报
回复
var conditions=document.getElementByName("conditions[]");
for(var i=0;i<condition.length;i++){
if(condition[i].checked=true)
; //do something
}

那一个也类似

87,917

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧