如何使两组复选框相互排斥

huangapple go评论53阅读模式
英文:

How to make 2 groups of checkboxes mutually exlusive

问题

以下是您要翻译的代码部分:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.3.slim.js" integrity="sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc" crossorigin="anonymous"></script>

    <title>Document</title>
</head>
<body>
    <div id="div1" style="border:1px solid blue; width:300px;">
        <input class="sev" id="chk1" type="checkbox">
        <label for="chk1">Check 1</label>
        <input class="sev" id="chk2" type="checkbox">
        <label for="chk2">Check 2</label>
        <input class="sev" id="chk3" type="checkbox">
        <label for="chk3">Check 3</label>
    </div>

    <div id="div2" class="orphan" style="border:1px solid green; width:300px; margin-top:50px;">
        <input class="orp" id="chk4" type="checkbox">
        <label for="chk4">Check 4</label>
        <input class="orp" id="chk5" type="checkbox">
        <label for="chk5">Check 5</label>
        <input class="orp" id="chk6" type="checkbox">
        <label for="chk6">Check 6</label>
    </div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.3.slim.js" integrity="sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc" crossorigin="anonymous"></script>

    <title>Document</title>
</head>
<body>
    <div id="div1" style="border:1px solid blue; width:300px;">
        <input id="chk1" type="checkbox">
        <label for="chk1">Check 1</label>
        <input id="chk2" type="checkbox">
        <label for="chk2">Check 2</label>
        <input id="chk3" type="checkbox">
        <label for="chk3">Check 3</label>
    </div>

    <div id="div2" style="border:1px solid green; width:300px; margin-top:50px;">
        <input id="chk4" type="checkbox">
        <label for="chk4">Check 4</label>
        <input id="chk5" type="checkbox">
        <label for="chk5">Check 5</label>
        <input id="chk6" type="checkbox">
        <label for="chk6">Check 6</label>
    </div>
</body>
</html>
$(document).ready(function(){
    $('.sev').on('change',function(){
        var isChecked = ($('.sev:checkbox:checked').length > 0) ? true : false;  
        $('#div2 input').prop('disabled', isChecked);
    });

    $('.orp').on('change',function(){
        var isChecked = ($('.orp:checkbox:checked').length > 0) ? true : false;  
        $('#div1 input').prop('disabled', isChecked);
    });
});

如您所需,我已提供了翻译的代码部分,没有包含额外的内容。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.slim.js&quot; integrity=&quot;sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;div1&quot; style=&quot;border:1px solid blue; width:300px;&quot;&gt;
&lt;input class=&quot;sev&quot; id=&quot;chk1&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk1&quot;&gt;Check 1&lt;/label&gt;
&lt;input class=&quot;sev&quot; id=&quot;chk2&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk2&quot;&gt;Check 2&lt;/label&gt;
&lt;input class=&quot;sev&quot; id=&quot;chk3&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk3&quot;&gt;Check 3&lt;/label&gt;
&lt;/div&gt;
&lt;div id=&quot;div2&quot; class=&quot;orphan&quot;  style=&quot;border:1px solid green; width:300px; margin-top:50px;&quot;&gt;
&lt;input class=&quot;orp&quot; id=&quot;chk4&quot;  type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk4&quot;&gt;Check 4&lt;/label&gt;
&lt;input class=&quot;orp&quot; id=&quot;chk5&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk5&quot;&gt;Check 5&lt;/label&gt;
&lt;input class=&quot;orp&quot; id=&quot;chk6&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk6&quot;&gt;Check 6&lt;/label&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

Let's say I have 2 divs with 3 checkboxes in each.

My goal is when user selects a checkbox in one of the groups, I want to disable the other group so no selections can be made. However I want to allow users to select as many checkboxes in one group as they want.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.slim.js&quot; integrity=&quot;sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;div1&quot;  style=&quot;border:1px solid blue; width:300px;&quot;&gt;
&lt;input id=&quot;chk1&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk1&quot;&gt;Check 1&lt;/label&gt;
&lt;input id=&quot;chk2&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk2&quot;&gt;Check 2&lt;/label&gt;
&lt;input id=&quot;chk3&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk3&quot;&gt;Check 3&lt;/label&gt;
&lt;/div&gt;
&lt;div id=&quot;div2&quot;  style=&quot;border:1px solid green; width:300px; margin-top:50px;&quot;&gt;
&lt;input id=&quot;chk4&quot;  type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk4&quot;&gt;Check 4&lt;/label&gt;
&lt;input id=&quot;chk5&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk5&quot;&gt;Check 5&lt;/label&gt;
&lt;input id=&quot;chk6&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk6&quot;&gt;Check 6&lt;/label&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

So if user checks chk1 in div1, disable all checkboxes in dev2.

I honestly am struggling with this and have no clue how to do this.

Okay I figured it out. It's been about 2 years since i touched Jquery/JS so forgive me.

I put this together and it seems to work:

 $(document).ready(function(){
$(&#39;.sev&#39;).on(&#39;change&#39;,function(){
var isChecked = ($(&#39;.sev:checkbox:checked&#39;).length &gt; 0) ? true : false;  
$(&#39;#div2 input&#39;).prop(&#39;disabled&#39;,isChecked);
});
$(&#39;.orp&#39;).on(&#39;change&#39;,function(){
var isChecked = ($(&#39;.orp:checkbox:checked&#39;).length &gt; 0) ? true : false;  
$(&#39;#div1 input&#39;).prop(&#39;disabled&#39;,isChecked);
});
});

I guess my question would be how can I make this into a single handler? so I don't have 2 separate handlres for 2 classes.

答案1

得分: 1

以下是使用jQuery执行此操作的方法;基本上监听所有复选框上的更改事件。一旦事件发生:

  • 确定发生此事件的div - thisDiv
  • 然后派生其他div - otherDiv
  • 根据thisDiv中是否有(或没有)任何选中的复选框设置otherDiv的复选框的禁用属性。

就是这样!

$('#div1 :checkbox, #div2 :checkbox').on('change', function(e) {
    const thisDiv = $(this).parent();
    const otherDiv = $('#div1,#div2').not(thisDiv);
    otherDiv.find(':checkbox').prop('disabled', thisDiv.find(':checkbox:checked').length);
});

演示 ...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.6.3.slim.js" integrity="sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc" crossorigin="anonymous"></script>

    <title>Document</title>
</head>
<body>
    <div id="div1" style="border:1px solid blue; width:300px;">
        <input id="chk1" type="checkbox">
        <label for="chk1">Check 1</label>
        <input id="chk2" type="checkbox">
        <label for="chk2">Check 2</label>
        <input id="chk3" type="checkbox">
        <label for="chk3">Check 3</label>
    </div>

    <div id="div2" style="border:1px solid green; width:300px; margin-top:50px;">
        <input id="chk4" type="checkbox">
        <label for="chk4">Check 4</label>
        <input id="chk5" type="checkbox">
        <label for="chk5">Check 5</label>
        <input id="chk6" type="checkbox">
        <label for="chk6">Check 6</label>
    </div>

</body>
</html>
英文:

Here is how to do it with jQuery; basically listen for the change event on all checkboxes. Once the event occurs:

  • determine the div in which this occurred - thisDiv;
  • then derive the other div - otherDiv
  • Set the checkboxes of otherDiv's disabled property based on whether there are (or not) any checked checkboxes in thisDiv

That's it!

$(&#39;#div1 :checkbox, #div2 :checkbox&#39;).on(&#39;change&#39;, function(e) {
const thisDiv = $(this).parent();
const otherDiv = $(&#39;#div1,#div2&#39;).not(thisDiv);
otherDiv.find(&#39;:checkbox&#39;).prop(&#39;disabled&#39;, thisDiv.find(&#39;:checkbox:checked&#39;).length);
});

DEMO ...

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#div1 :checkbox, #div2 :checkbox&#39;).on(&#39;change&#39;, function(e) {
const thisDiv = $(this).parent();
const otherDiv = $(&#39;#div1,#div2&#39;).not(thisDiv);
otherDiv.find(&#39;:checkbox&#39;).prop(&#39;disabled&#39;, thisDiv.find(&#39;:checkbox:checked&#39;).length);
});

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.3.slim.js&quot; integrity=&quot;sha256-DKU1CmJ8kBuEwumaLuh9Tl/6ZB6jzGOBV/5YpNE2BWc=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;div1&quot;  style=&quot;border:1px solid blue; width:300px;&quot;&gt;
&lt;input id=&quot;chk1&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk1&quot;&gt;Check 1&lt;/label&gt;
&lt;input id=&quot;chk2&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk2&quot;&gt;Check 2&lt;/label&gt;
&lt;input id=&quot;chk3&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk3&quot;&gt;Check 3&lt;/label&gt;
&lt;/div&gt;
&lt;div id=&quot;div2&quot;  style=&quot;border:1px solid green; width:300px; margin-top:50px;&quot;&gt;
&lt;input id=&quot;chk4&quot;  type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk4&quot;&gt;Check 4&lt;/label&gt;
&lt;input id=&quot;chk5&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk5&quot;&gt;Check 5&lt;/label&gt;
&lt;input id=&quot;chk6&quot; type=&quot;checkbox&quot;&gt;
&lt;label for=&quot;chk6&quot;&gt;Check 6&lt;/label&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 0

如果您将 "chk1" 类添加到您的 div1 输入元素中,此 JQuery 代码应该能够实现您想要的效果:

$(".chk1").on("input", function() {
    if ($(this)[0].checked) {
        $("#div2 input").prop("disabled", true);
    } else {
        $("#div2 input").prop("disabled", false);
    }
});
英文:

If you add "chk1" class to your div1 inputs this JQuery code should achieve what you're looking for:

            $(&quot;.chk1&quot;).on(&quot;input&quot;, function() {
if ($(this)[0].checked) {
$(&quot;#div2 input&quot;).prop(&quot;disabled&quot;, true);
} else {
$(&quot;#div2 input&quot;).prop(&quot;disabled&quot;, false);
}
});

答案3

得分: 0

$(document).ready(function(){

$('.sev').on('change',function(){
var isChecked = ($('.sev:checkbox:checked').length > 0) ? true : false;  
$('#div2 input').prop('disabled',isChecked);
});
$('.orp').on('change',function(){
var isChecked = ($('.orp:checkbox:checked').length > 0) ? true : false;  
$('#div1 input').prop('disabled',isChecked);
});

});

英文:
$(document).ready(function(){
$(&#39;.sev&#39;).on(&#39;change&#39;,function(){
var isChecked = ($(&#39;.sev:checkbox:checked&#39;).length &gt; 0) ? true : false;  
$(&#39;#div2 input&#39;).prop(&#39;disabled&#39;,isChecked);
});
$(&#39;.orp&#39;).on(&#39;change&#39;,function(){
var isChecked = ($(&#39;.orp:checkbox:checked&#39;).length &gt; 0) ? true : false;  
$(&#39;#div1 input&#39;).prop(&#39;disabled&#39;,isChecked);
});
});

huangapple
  • 本文由 发表于 2023年2月14日 09:36:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定