[英]JQuery - Allow Specific 2 Checkboxes to be Selected
I only want 1 checkbox to be selected - UNLESS its checkbox 3 AND 4 - then I want to allow these 2 checkboxes to be selected.我只想选择 1 个复选框 - 除非它的复选框 3 和 4 - 然后我想允许选择这两个复选框。 This is the only time I want 2 checkboxes allowed.
这是我唯一一次希望允许 2 个复选框。
I have a working example of only allowing 1 checkbox.我有一个只允许 1 个复选框的工作示例。 see the jsfiddle... https://jsfiddle.net/rbla/s1setkfe/3/
看 jsfiddle ... https://jsfiddle.net/rbla/s1setkfe/3/
I need to allow #3 and #4 to be selected我需要允许选择 #3 和 #4
$(function() { $('#submit').click(function() { checked = $("input[type=checkbox]:checked").length; if (!checked) { alert("You must check at least one reason."); return false; } }); }); // No more than 1 checkbox allowed var limit = 1; $('input.sing-chbx').on('change', function(evt) { if ($("input[name='choice[]']:checked").length > limit) { this.checked = false; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" name="formname" method="post" autocomplete="off" id="update"> <div class="group" style="margin:0.5em 0;"> <div> <div id="one"> <input type="checkbox" class="sing-chbx" id="choice" name="choice[]" value="01"> <label>One</label><br/> </div> <div id="two"> <input type="checkbox" class="sing-chbx" name="choice[]" value="02"> <label>Two</label><br/> </div> <div id="three"> <input type="checkbox" class="sing-chbx" name="choice[]" value="03"> <label>Three</label><br/> </div> <div id="four"> <input type="checkbox" class="sing-chbx" name="choice[]" value="04"> <label>Four</label><br/> </div> <div id="five"> <input type="checkbox" class="sing-chbx" name="choice[]" value="05"> <label>Five</label><br/> </div> </div> </div> <input type="submit" id="submit" value="Confirm Submission"> </form>
I have created a fiddle for you demonstrating my solution.我为您创建了一个小提琴来演示我的解决方案。
I changed the way you're handling this to be more visual to the user with what is happening by actually disabling the other checkboxes.我改变了你处理这个问题的方式,通过实际禁用其他复选框来让用户更直观地了解正在发生的事情。
I added new classes to all of the checkboxes that only allow one selection, and added a separate class to the checkboxes that allow two selections.我向所有只允许一个选择的复选框添加了新类,并在允许两个选择的复选框中添加了一个单独的类。
After that you just need to check the class of the clicked checkbox, and disable the others depending on whether or not it was a select-one
or select-two
checkbox:之后,您只需要检查单击复选框的类,并根据它是
select-one
还是select-two
复选框来禁用其他类:
var canOnlySelectOne = $(this).hasClass("select-one");
if (canOnlySelectOne) {
$(".sing-chbx").not(this).attr("disabled", this.checked);
} else if ($(this).hasClass("select-two")) {
if ($(".select-two:checked").length > 0) {
$(".select-one").attr("disabled", true);
} else {
$(".select-one").attr("disabled", this.checked);
}
}
We simply enable/disable the other checkboxes based on whether or not the clicked one ( this
) is checked or not.我们只是根据是否选中了点击的复选框(
this
)来启用/禁用其他复选框。 If the checkbox has a class of select-two
then we check if any of the select-two
checkboxes are checked, and act accordingly.如果复选框有一个
select-two
一的类,那么我们检查是否有任何select-two
复选框被选中,并采取相应的行动。
Here is the updated code:这是更新后的代码:
$(function() { $('#submit').click(function() { checked = $("input[type=checkbox]:checked").length; if (!checked) { alert("You must check at least one reason."); return false; } }); }); // No more than 1 checkbox allowed except 3 & 4 $('input.sing-chbx').on('change', function(evt) { var me = $(this).val(); $('input.sing-chbx').each(function(ix){ var el = $(this); if(el.val()!= me) { if(me == "03") { // case 1: me == '03' - disable all but '04' if( el.val() != "04") { el.prop('checked', false); } } else if(me == "04") { // case 2: me == '04' - disable all but '03' if(el.val() != "03") { el.prop('checked', false); } } else { // otherwise disable all el.prop('checked', false); } } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" name="formname" method="post" autocomplete="off" id="update"> <div class="group" style="margin:0.5em 0;"> <div> <div id="one"> <input type="checkbox" class="sing-chbx" id="choice" name="choice[]" value="01"> <label>One</label><br/> </div> <div id="two"> <input type="checkbox" class="sing-chbx" name="choice[]" value="02"> <label>Two</label><br/> </div> <div id="three"> <input type="checkbox" class="sing-chbx" name="choice[]" value="03"> <label>Three</label><br/> </div> <div id="four"> <input type="checkbox" class="sing-chbx" name="choice[]" value="04"> <label>Four</label><br/> </div> <div id="five"> <input type="checkbox" class="sing-chbx" name="choice[]" value="05"> <label>Five</label><br/> </div> </div> </div> <input type="submit" id="submit" value="Confirm Submission"> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.