Javascript 如何验证在每组复选框中至少选中一个复选框

Javascript 如何验证在每组复选框中至少选中一个复选框,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我有一大组复选框,由两个表分隔。在这两组复选框中,我希望用户在提交表单之前至少选中两组中的一个复选框(因此,根据我的代码,至少有一个复选框位于成本下,一个复选框位于福利下)。我希望这是有道理的。我的代码如下: <body> <h2 align="center">Cost/Benefit Matrix</h2> <script type="text/javascript" src="http://localhost/mytesting/jquery-2.1

我有一大组复选框,由两个表分隔。在这两组复选框中,我希望用户在提交表单之前至少选中两组中的一个复选框(因此,根据我的代码,至少有一个复选框位于成本下,一个复选框位于福利下)。我希望这是有道理的。我的代码如下:

<body>
<h2 align="center">Cost/Benefit Matrix</h2>
<script type="text/javascript" src="http://localhost/mytesting/jquery-2.1.4.js"></script>


<style type="text/css">
p
    {
        font-family: "Arial";
        align: center;
    }
h2
    {
        font-family: "Arial";
    }
</style>


<p>
<table border="1" align="center">

<tbody>
<tr>
    <th><b>COST</b></th>
    <th colspan="3">Reduced Cost</th>
    <th>Neutral</th>
    <th colspan="3">Increased Cost</th>
    <th>Don't Know</th>
</tr>
<tr>
    <th></th>
    <th>High</th>
    <th>Medium</th>
    <th>Low</th>
    <th>No effect</th>
    <th>Low</th>
    <th>Medium</th>
    <th>High</th>
    <th></th>
</tr>
<tr>
    <td>Capital cost</td>
    <td><input type="checkbox" id="matrix1" value="1"></td>
    <td><input type="checkbox" id="matrix2" value="1"></td>
    <td><input type="checkbox" id="matrix3" value="1"></td>
    <td><input type="checkbox" id="matrix4" value="1"></td>
    <td><input type="checkbox" id="matrix5" value="1"></td>
    <td><input type="checkbox" id="matrix6" value="1"></td>
    <td><input type="checkbox" id="matrix7" value="1"></td>
    <td><input type="checkbox" id="matrix8" value="1"></td>

</tr>
<tr>
    <td>Clinical operating cost</td>
    <td><input type="checkbox" id="matrix9" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix10" value="1"></td>
    <td><input type="checkbox" id="matrix11" value="1"></td>
    <td><input type="checkbox" id="matrix12" value="1"></td>
    <td><input type="checkbox" id="matrix13" value="1"></td>
    <td><input type="checkbox" id="matrix14" value="1"></td>
    <td><input type="checkbox" id="matrix15" value="1"></td>
    <td><input type="checkbox" id="matrix16" value="1"></td>

</tr>

<tr>
    <td>Facility operating cost</td>
    <td><input type="checkbox" id="matrix17" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix18" value="1"></td>
    <td><input type="checkbox" id="matrix19" value="1"></td>
    <td><input type="checkbox" id="matrix20" value="1"></td>
    <td><input type="checkbox" id="matrix21" value="1"></td>
    <td><input type="checkbox" id="matrix22" value="1"></td>
    <td><input type="checkbox" id="matrix23" value="1"></td>
    <td><input type="checkbox" id="matrix24" value="1"></td>
</tr>

<tr>
    <td>Energy cost</td>
    <td><input type="checkbox" id="matrix25" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix26" value="1"></td>
    <td><input type="checkbox" id="matrix27" value="1"></td>
    <td><input type="checkbox" id="matrix28" value="1"></td>
    <td><input type="checkbox" id="matrix29" value="1"></td>
    <td><input type="checkbox" id="matrix30" value="1"></td>
    <td><input type="checkbox" id="matrix31" value="1"></td>
    <td><input type="checkbox" id="matrix32" value="1"></td>
</tr>

<br>
<br>

<table border="1" align="center">

<tbody>
<tr>
    <th><b>BENEFIT</b></th>
    <th colspan="3">Negative Impact</th>
    <th>Neutral</th>
    <th colspan="3">Positive Impact</th>
    <th>Don't Know</th>
</tr>
<tr>
    <th></th>
    <th>High</th>
    <th>Medium</th>
    <th>Low</th>
    <th>No effect</th>
    <th>Low</th>
    <th>Medium</th>
    <th>High</th>
    <th></th>
</tr>
<tr>
    <td>Patient/staff safety</td>

    <td><input type="checkbox" id="matrix33" value="1"></td>
    <td><input type="checkbox" id="matrix34" value="1"></td>
    <td><input type="checkbox" id="matrix35" value="1"></td>
    <td><input type="checkbox" id="matrix36" value="1"></td>
    <td><input type="checkbox" id="matrix37" value="1"></td>
    <td><input type="checkbox" id="matrix38" value="1"></td>
    <td><input type="checkbox" id="matrix39" value="1"></td>
    <td><input type="checkbox" id="matrix40" value="1"></td>


</tr>
<tr>
    <td>Fire/life safety</td>
    <td><input type="checkbox" id="matrix41" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix42" value="1"></td>
    <td><input type="checkbox" id="matrix43" value="1"></td>
    <td><input type="checkbox" id="matrix44" value="1"></td>
    <td><input type="checkbox" id="matrix45" value="1"></td>
    <td><input type="checkbox" id="matrix46" value="1"></td>
    <td><input type="checkbox" id="matrix47" value="1"></td>
    <td><input type="checkbox" id="matrix48" value="1"></td>

</tr>

<tr>
    <td>Clinical quality of care</td>
    <td><input type="checkbox" id="matrix49" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix50" value="1"></td>
    <td><input type="checkbox" id="matrix51" value="1"></td>
    <td><input type="checkbox" id="matrix52" value="1"></td>
    <td><input type="checkbox" id="matrix53" value="1"></td>
    <td><input type="checkbox" id="matrix54" value="1"></td>
    <td><input type="checkbox" id="matrix55" value="1"></td>
    <td><input type="checkbox" id="matrix56" value="1"></td>
</tr>

<tr>
    <td>Patient/resident experience</td>
    <td><input type="checkbox" id="matrix57" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix58" value="1"></td>
    <td><input type="checkbox" id="matrix59" value="1"></td>
    <td><input type="checkbox" id="matrix60" value="1"></td>
    <td><input type="checkbox" id="matrix61" value="1"></td>
    <td><input type="checkbox" id="matrix62" value="1"></td>
    <td><input type="checkbox" id="matrix63" value="1"></td>
    <td><input type="checkbox" id="matrix64" value="1"></td>
</tr>

<tr>
    <td>Operational efficiency</td>
    <td><input type="checkbox" id="matrix65" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix66" value="1"></td>
    <td><input type="checkbox" id="matrix67" value="1"></td>
    <td><input type="checkbox" id="matrix68" value="1"></td>
    <td><input type="checkbox" id="matrix69" value="1"></td>
    <td><input type="checkbox" id="matrix70" value="1"></td>
    <td><input type="checkbox" id="matrix71" value="1"></td>
    <td><input type="checkbox" id="matrix72" value="1"></td>
</tr>

<tr>
    <td>Sustainability</td>
    <td><input type="checkbox" id="matrix73" value="1" style="vertical-align: middle"></td>
    <td><input type="checkbox" id="matrix74" value="1"></td>
    <td><input type="checkbox" id="matrix75" value="1"></td>
    <td><input type="checkbox" id="matrix76" value="1"></td>
    <td><input type="checkbox" id="matrix77" value="1"></td>
    <td><input type="checkbox" id="matrix78" value="1"></td>
    <td><input type="checkbox" id="matrix79" value="1"></td>
    <td><input type="checkbox" id="matrix80" value="1"></td>
</tr>

<script type="text/javascript">
    $('input[type="checkbox"]').on('change', function() {

      // uncheck sibling checkboxes (checkboxes on the same row)
      $(this).closest('tr').find('input').not(this).prop('checked', false);

      // uncheck checkboxes in the same column
      $('div').find('input[type="checkbox"]:eq(' + $(this).index() + ')').not(this).prop('checked', false);

    });
</script>
</tbody>
</tbody>
</p>

成本/效益矩阵
P
{
字体系列:“Arial”;
对齐:居中;
}
氢
{
字体系列:“Arial”;
}

成本
降低成本
中立的
增加的成本
不知道
高
中等
低
无效
低
中等
高
资本成本
临床运行成本
设施运营成本
能源成本


利益 负面影响 中立的 积极影响 不知道 高 中等 低 无效 低 中等 高 病人/员工安全 消防/生命安全 临床护理质量 病人/住院医师经验 运行效率 持续性 $('input[type=“checkbox”]”)。在('change',function()上{ //取消选中同级复选框(同一行上的复选框) $(this).closest('tr').find('input').not(this).prop('checked',false)); //取消选中同一列中的复选框 $('div').find('input[type=“checkbox”]:eq('+$(this.index()+'))。not(this.prop('checked',false)); });

您可以在每个复选框中添加一个类(如
.checkbox
),然后在提交前执行类似操作-

if ($(".checkbox:checked").length > 0) {
    //perform submit
}
else{
    alert("check at least one checkbox!");
}
您可以在每个复选框中添加一个类(如
.checkbox
),然后在提交之前执行类似操作-

if ($(".checkbox:checked").length > 0) {
    //perform submit
}
else{
    alert("check at least one checkbox!");
}
您可以在每个复选框中添加一个类(如
.checkbox
),然后在提交之前执行类似操作-

if ($(".checkbox:checked").length > 0) {
    //perform submit
}
else{
    alert("check at least one checkbox!");
}
您可以在每个复选框中添加一个类(如
.checkbox
),然后在提交之前执行类似操作-

if ($(".checkbox:checked").length > 0) {
    //perform submit
}
else{
    alert("check at least one checkbox!");
}
您还可以使用“.is:以提高可读性。

示例:

您也可以使用“.is:以提高可读性。

示例:

您也可以使用“.is:以提高可读性。

示例:

您也可以使用“.is:以提高可读性。


示例:

首先,您的HTML格式不正确。在开始新的表元素之前,应该先关闭一个表元素

然后从按id或至少按类区分表开始,例如:

<table id="cost-table" border="1" align="center">

其他类型也一样。

首先,您的HTML格式不正确。在开始新的表元素之前,应该先关闭一个表元素

然后从按id或至少按类区分表开始,例如:

<table id="cost-table" border="1" align="center">

其他类型也一样。

首先,您的HTML格式不正确。在开始新的表元素之前,应该先关闭一个表元素

然后从按id或至少按类区分表开始,例如:

<table id="cost-table" border="1" align="center">

其他类型也一样。

首先,您的HTML格式不正确。在开始新的表元素之前,应该先关闭一个表元素

然后从按id或至少按类区分表开始,例如:

<table id="cost-table" border="1" align="center">

其他类型也一样。

您考虑过使用radiobutton吗?我试图将您的代码拉入JSFIDLE以帮助您解决问题,但您的分数是。。。看起来,到处都是。有一些标记以奇怪的区域结尾,很难对其进行处理。zfrisch-再试一次,当不需要任何标记时,我有div标记。您考虑过使用radiobutton吗?我试图将您的代码拉入JSFIDLE以帮助您,但您的标记是。。。看起来,到处都是。有一些标记以奇怪的区域结尾,很难对其进行处理。zfrisch-再试一次,当不需要任何标记时,我有div标记。您考虑过使用radiobutton吗?我试图将您的代码拉入JSFIDLE以帮助您,但您的标记是。。。看起来,到处都是。有一些标记以奇怪的区域结尾,很难对其进行处理。zfrisch-再试一次,当不需要任何标记时,我有div标记。您考虑过使用radiobutton吗?我试图将您的代码拉入JSFIDLE以帮助您,但您的标记是。。。看起来,到处都是。有一些标签以奇怪的区域结尾,很难对其进行处理。zfrisch-再试一次,我有div标签,但不需要任何标签