[英]When one checkbox is checked in a group, disable & uncheck all other checkboxes
So in the code below, 因此,在下面的代码中,
function listen(element, event, callback) { if (element.attachEvent) { element.attachEvent('on' + event, callback); } else { element.addEventListener(event, callback); } } var form = document.querySelector('#myForm'); listen(form, 'click', function (event) { var checkBoxes, i, checked, target; target = event.srcElement || event.target; if (target.getAttribute('name') === 'check1') { checkBoxes = form.querySelectorAll('input[name="check2"]'); checked = target.checked; for (i = 0; i < checkBoxes.length; i++) { checkBoxes[i].disabled = checked && checkBoxes[i] !== target; } } });
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <form id="myForm"> <table> <tr> <td> <input type='checkbox' name='check1' value = '1'>Report 1 </td> <td> <input type='checkbox' name='check1' value = '2'>R1 Option 1 </td> <td> <input type='checkbox' name='check1' value = '3'>R1 Option 2 </td> <td> <input type='checkbox' name='check1' value = '4'>R1 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check2' value = '5'>Report 2 </td> <td> <input type='checkbox' name='check2' value = '6'>R2 Option 1 </td> <td> <input type='checkbox' name='check2' value = '7'>R2 Option 2 </td> <td> <input type='checkbox' name='check2' value = '8'>R2 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check3' value = '5'>Report 3 </td> <td> <input type='checkbox' name='check3' value = '6'>R3 Option 1 </td> <td> <input type='checkbox' name='check3' value = '7'>R3 Option 2 </td> <td> <input type='checkbox' name='check3' value = '8'>R3 Option 3 </td> </tr> </table> </form> </body> </html>
Report 1 is checked, Report 2 and Report 3 as well as all the options for Report 2 and Report 3 should be un-checked and not selectable. 检查报告1,报告2和报告3以及报告2和报告3的所有选项都应该是未检查的,不可选择。 I only want the user to be able to select one report at a time, with the options for that report only to be available for selection.
我只希望用户一次只能选择一个报告,而该报告的选项仅可供选择。 Any assistance is much appreciated.
非常感谢任何帮助。
Snippet:- 片段: -
$(document).ready(function () { $('input[type="checkbox"]').click(function () { if ($(this).prop("checked") == true) { $('input').on('change', function () { $('input').not(this).attr('disabled', true); }); } else if ($(this).prop("checked") == false) { $('input').on('change', function () { $('input').not(this).attr('disabled', false); }); } }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <input type='checkbox' name='check1' value = '1'>Report 1 </td> <td> <input type='checkbox' name='check1' value = '2'>R1 Option 1 </td> <td> <input type='checkbox' name='check1' value = '3'>R1 Option 2 </td> <td> <input type='checkbox' name='check1' value = '4'>R1 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check2' value = '5'>Report 2 </td> <td> <input type='checkbox' name='check2' value = '6'>R2 Option 1 </td> <td> <input type='checkbox' name='check2' value = '7'>R2 Option 2 </td> <td> <input type='checkbox' name='check2' value = '8'>R2 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check3' value = '5'>Report 3 </td> <td> <input type='checkbox' name='check3' value = '6'>R3 Option 1 </td> <td> <input type='checkbox' name='check3' value = '7'>R3 Option 2 </td> <td> <input type='checkbox' name='check3' value = '8'>R3 Option 3 </td> </tr> </table>
Here is a simple validation 这是一个简单的验证
$("#myForm").find("input[type='checkbox']").on("click", function(){ if ($(this).is(":checked")) { $("#myForm").find("input[type='checkbox']").not($(this)).prop("disabled", true) }else { $("#myForm").find("input[type='checkbox']").prop("disabled", false) } });
<!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> <form id="myForm"> <table> <tr> <td> <input type='checkbox' name='check1' value = '1'>Report 1 </td> <td> <input type='checkbox' name='check1' value = '2'>R1 Option 1 </td> <td> <input type='checkbox' name='check1' value = '3'>R1 Option 2 </td> <td> <input type='checkbox' name='check1' value = '4'>R1 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check2' value = '5'>Report 2 </td> <td> <input type='checkbox' name='check2' value = '6'>R2 Option 1 </td> <td> <input type='checkbox' name='check2' value = '7'>R2 Option 2 </td> <td> <input type='checkbox' name='check2' value = '8'>R2 Option 3 </td> </tr> <tr> <td> <input type='checkbox' name='check3' value = '5'>Report 3 </td> <td> <input type='checkbox' name='check3' value = '6'>R3 Option 1 </td> <td> <input type='checkbox' name='check3' value = '7'>R3 Option 2 </td> <td> <input type='checkbox' name='check3' value = '8'>R3 Option 3 </td> </tr> </table> </form> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.