如果从可能的复选框列表中只选中了一个特定的复选框,则返回 Javascript
Javascript returning if only one certain checkbox is checked out of a list of possible checkboxes
我有一个复选框列表,需要选中一个或两个特定的框才能返回true,但是如果只选中了所需的框而没有其他框,我不确定如何查找。
复选框的 HTML 如下所示:
<table style="width:135px; height:200px; margin: 0 auto; margin-top: -200px;">
<tr>
<td><input type="checkbox" class="f1s1c"></td>
<td><input type="checkbox" class="f1s2"></td>
<td><input type="checkbox" class="f1s3"></td>
<td><input type="checkbox" class="f1s4"></td>
</tr>
<tr>
<td><input type="checkbox" class="f2s1"></td>
<td><input type="checkbox" class="f2s2"></td>
<td><input type="checkbox" class="f2s3"></td>
<td><input type="checkbox" class="f2s4"></td>
</tr>
<tr>
<td><input type="checkbox" class="f3s1"></td>
<td><input type="checkbox" class="f3s2"></td>
<td><input type="checkbox" class="f3s3"></td>
<td><input type="checkbox" id="cCorrect1"></td>
</tr>
<tr>
<td><input type="checkbox" class="f4s1"></td>
<td><input type="checkbox" class="f4s2"></td>
<td><input type="checkbox" class="f4s3"></td>
<td><input type="checkbox" class="f4s4"></td>
</tr>
</table>
如您所见,有许多可能的 chekbox,但在这种情况下,必须选中 cCorrect1 才能使 javascript 返回 true。所有其他复选框都作为类,因为我有多个遵循相同结构的表。
目前,如果选中cCorrect1,我的Javascript返回true,但如果同时选中任何其他框,则显然也会返回true。
我的Javascript:
//Quiz Functions
$("#checkC").click(function(){
if(cCorrect1.checked){
cCorrect = true;
}else if(cCorrect1.checked == false){
cCorrect = false;
}
});
使用检查复选框并找出何时选中 cCorrect1 的数组会起作用吗?我认为这可能走在正确的轨道上,但我不知道该怎么做。
任何意见和帮助都非常感谢。
假设您有办法找到正确的复选框集(所有复选框上的共享类等),您可以计算列表中的复选框数量。如果它是1
,并且您的目标框被选中,那么您很好。
在此示例中,我向包含复选框的table
中添加了一个id
,以使其更易于查找。删除了样式,使表格可见。
$("#checkC").click(function(){
// the one we want
var cCorrect1 = $('#cCorrect1');
// all checked checkboxes in the table
var checks = $('#boxes input[type=checkbox]:checked');
var cCorrect = cCorrect1.prop('checked') && (checks.length == 1);
alert(cCorrect ? "correct" : "incorrect");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="boxes" >
<tr>
<td><input type="checkbox" class="f1s1c"></td>
<td><input type="checkbox" class="f1s2"></td>
<td><input type="checkbox" class="f1s3"></td>
<td><input type="checkbox" class="f1s4"></td>
</tr>
<tr>
<td><input type="checkbox" class="f2s1"></td>
<td><input type="checkbox" class="f2s2"></td>
<td><input type="checkbox" class="f2s3"></td>
<td><input type="checkbox" class="f2s4"></td>
</tr>
<tr>
<td><input type="checkbox" class="f3s1"></td>
<td><input type="checkbox" class="f3s2"></td>
<td><input type="checkbox" class="f3s3"></td>
<td><input type="checkbox" id="cCorrect1"></td>
</tr>
<tr>
<td><input type="checkbox" class="f4s1"></td>
<td><input type="checkbox" class="f4s2"></td>
<td><input type="checkbox" class="f4s3"></td>
<td><input type="checkbox" class="f4s4"></td>
</tr>
</table>
<button id="checkC">check</button>
您可以做的是创建一个正确答案数组,发现每行的选定答案,并确定它是"正确"还是"不正确"。
您可以使用此代码返回复选框状态数组(0 表示未选中,1 表示选中),并决定从那里执行的操作:
var $table = this.$('tbody');
$(".resultBtn").click(function(){
var outStr = '';
for(var i= 0,len=$table.children().length;i<len;i++){
var $tr = $table.children().eq(i);
outStr += 'row' + (i+1) + ' checked boxes:[';
for(var j= 0;j<$tr.children().length;j++){
var $td = $tr.children().eq(j);
if($td.find(':checked').length > 0 ){
$td.addClass('selected');
outStr += '1,';
} else{
outStr += '0,';
}
if(j==$tr.children().length-1) outStr = outStr.substring(0, outStr.length - 1);
}
outStr += ']';
}
alert(outStr);
});
例:http://jsfiddle.net/y3yp4ag1/2/
相关文章:
- 复选框:一次只允许单击一个复选框
- jquery从2个json字符串构建一个复选框表单
- 聚合物铁形式验证至少选中一个复选框
- 我有多个复选框,并希望为其中 2 个制定规则,以便如果我选择 1 个,则无法选择另一个
- 如何从多个复选框中获取最后一个值
- 选中复选框的Jquery/Javascript问题使用输入框操作将行从一个表添加到另一个表
- 在单击一个或多个复选框的同时调用iframe
- 如何创建只允许用户在选中前一个复选框的情况下选择复选框的复选框验证
- 复选框一次选中一个不在gridview中工作
- 如果选中了以下复选框,请选中上一个复选框
- 复选框一次选中一个不起作用
- 如何创建一个新的<李>带有复选框或<a>在里面
- 在一组复选框中,一次只允许选择一个
- 如何通过jQuery选中所有复选框并在单击一个复选框时取消选择
- 如何发现至少有一个复选框被选中或没有使用javascript
- 在mvc4中选择另一个复选框时禁用复选框
- 导航到另一个页面后,复选框状态会更改
- Javascript选中一个表中的所有复选框,该表的行具有样式显示标记
- 在服务器上创建一个复选框,使用 javascript 处理函数
- 选择一个从 mysql 返回选中/未选中复选框的对象