如果在可能的复选框列表中仅选中了某个复选框,则返回Javascript
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如果在可能的复选框列表中仅选中了某个复选框,则返回Javascript,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2949字,纯文字阅读大概需要5分钟。
内容图文
![如果在可能的复选框列表中仅选中了某个复选框,则返回Javascript](/upload/InfoBanner/zyjiaocheng/661/08ab0f5842094d8e85e68976275c66b2.jpg)
我有一个复选框列表,需要选中一个或两个特定的复选框才能返回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>
如您所见,有很多可能的复选框,但是在这种情况下,仅必须检查cCorrect1才能使javascript返回true.所有其他复选框都作为类,因为我有多个遵循相同结构的表.
当前,如果选中了cCorrect1,我的Javascript将返回true,但是如果还选中了任何其他框,则Javascript也显然将返回true.
我的Javascript:
//Quiz Functions
$("#checkC").click(function(){
if(cCorrect1.checked){
cCorrect = true;
}else if(cCorrect1.checked == false){
cCorrect = false;
}
});
是否可以使用检查复选框并找出何时选中cCorrect1的数组起作用?我认为这可能是在正确的轨道上,但我不知道该如何去做.
任何意见和帮助,我们将不胜感激.
解决方法:
假设您有办法找到正确的复选框集(所有复选框均处于共享类,等等),则可以计算列表中复选框的数量.如果它是1,并且您的目标框已选中,那就很好.
在此示例中,我向包含复选框的表添加了一个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>
内容总结
以上是互联网集市为您收集整理的如果在可能的复选框列表中仅选中了某个复选框,则返回Javascript全部内容,希望文章能够帮你解决如果在可能的复选框列表中仅选中了某个复选框,则返回Javascript所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。