Jquery 复选框已选中/隐藏 div 如果两个复选框已选中或未选中

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

Jquery Checkbox Checked Show / Hide Div if two checkboxes is check or unchecked

问题

以下是翻译好的部分:

#1:如果用户选中复选框"A"或"B",则显示"A_B" div元素。

#2:如果用户取消选中"A"复选框,但仍选中"B"复选框,则"A_B" div元素仍然显示。

#3:如果用户取消选中"A"和"B"复选框,那么只有"A_B" div元素将被隐藏。

现在,当我取消选中"A"或"B"复选框时,div元素将被隐藏,不应该切换。有没有什么改进的方法来实现这3个条件?

任何想法/解决方案都将是很大的帮助。对于遇到与我相同问题的一些用户来说,这也可能会很有用。

英文:

I have this simple jquery code for checkbox that checks via attributes. I know that there are many similarities for this answered already but the idea here is different.

I want to show the A_B div element for the following condition:

#1: User checked checkbox A or B then shows the A_B div element

#2: If user unchecked A checkbox but checkbox B is still checked. Then A_B div element is still showing.

*Note: This can be reversal for the situation*

#3 If user uncheckeds both A or B checkbox. Then only A_B div element will hide.

Right now. When I uncheck A or B checkbox. The div element will hide, should not suppose to toggle.
Is there anything here to improve. To achieve the following 3 conditions?

Any ideas / solutions would be a great help. Might be useful also to some users that encounters the same problem as mine. Thanks.

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

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

$(&#39;input[chck-type=&quot;a_b&quot;]&#39;).click(function(){
	if (
  	$(this).prop(&quot;checked&quot;) == true
  ) {
 	  $(&#39;.A_B&#39;).removeClass(&#39;hide&#39;);
  } else {
  	$(&#39;.A_B&#39;).addClass(&#39;hide&#39;);
  }
});

$(&#39;input[chck-type=&quot;c&quot;]&#39;).click(function(){
	if (
  	$(this).prop(&quot;checked&quot;) == true
  ) {
 	  $(&#39;.C_Box&#39;).removeClass(&#39;hide&#39;);
  } else {
  	$(&#39;.C_Box&#39;).addClass(&#39;hide&#39;);
  }
});

<!-- language: lang-css -->

.hide {display:none}

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;A&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;B&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;c&quot;&gt;C&lt;/p&gt;


&lt;div class=&#39;A_B hide&#39;&gt;
This box is for A or B.
&lt;/div&gt;

&lt;div class=&#39;C_Box hide&#39;&gt;
This box is for C.
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

您没有检查两个复选框是否都未选中。

$('[chck-type="a_b"]').click(function(){
    if ($(this).prop("checked") == true) {
        $('.A_B').removeClass('hide');
    } else if($('input[chck-type="a_b"]:checked').length === 0) {
        $('.A_B').addClass('hide');
    }
});

$('input[chck-type="c"]').click(function(){
    if ($(this).prop("checked") == true) {
        $('.C_Box').removeClass('hide');
    } else {
        $('.C_Box').addClass('hide');
    }
});
.hide {display:none}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><input type="checkbox" chck-type="a_b">A</p>
<p><input type="checkbox" chck-type="a_b">B</p>
<p><input type="checkbox" chck-type="c">C</p>

<div class='A_B hide'>
This box is for A or B.
</div>

<div class='C_Box hide'>
This box is for C.
</div>
英文:

You are not checking if both checkboxes are unchecked or not.

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

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

$(&#39;input[chck-type=&quot;a_b&quot;]&#39;).click(function(){
    if (
    $(this).prop(&quot;checked&quot;) == true
  ) {
      $(&#39;.A_B&#39;).removeClass(&#39;hide&#39;);
  } else if($(&#39;input[chck-type=&quot;a_b&quot;]&#39;)[0].checked == false &amp;&amp; $(&#39;input[chck-type=&quot;a_b&quot;]&#39;)[1].checked == false) {
    $(&#39;.A_B&#39;).addClass(&#39;hide&#39;);
  }
});

$(&#39;input[chck-type=&quot;c&quot;]&#39;).click(function(){
    if (
    $(this).prop(&quot;checked&quot;) == true
  ) {
      $(&#39;.C_Box&#39;).removeClass(&#39;hide&#39;);
  } else {
    $(&#39;.C_Box&#39;).addClass(&#39;hide&#39;);
  }
});

<!-- language: lang-css -->

.hide {display:none}

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;A&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;B&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;c&quot;&gt;C&lt;/p&gt;


&lt;div class=&#39;A_B hide&#39;&gt;
This box is for A or B.
&lt;/div&gt;

&lt;div class=&#39;C_Box hide&#39;&gt;
This box is for C.
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

$('[chck-type="a_b"]').click(function(){
    let checkboxes = $('[chck-type="a_b"]:checked').length;
    if (
    checkboxes != 0
  ) {
      $('.A_B').removeClass('hide');
  } else {
    $('.A_B').addClass('hide');
  }
});

$('[chck-type="c"]').click(function(){
    if (
    $(this).prop("checked") == true
  ) {
      $('.C_Box').removeClass('hide');
  } else {
    $('.C_Box').addClass('hide');
  }
});
.hide {display:none}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<p><input type="checkbox" chck-type="a_b">A</p>
<p><input type="checkbox" chck-type="a_b">B</p>
<p><input type="checkbox" chck-type="c">C</p>

<div class='A_B hide'>
This box is for A or B.
</div>

<div class='C_Box hide'>
This box is for C.
</div>
英文:

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

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

$(&#39;input[chck-type=&quot;a_b&quot;]&#39;).click(function(){
    let checkboxes = $(&#39;input[chck-type=&quot;a_b&quot;]:checked&#39;).length;
    if (
    checkboxes != 0
  ) {
      $(&#39;.A_B&#39;).removeClass(&#39;hide&#39;);
  } else {
    $(&#39;.A_B&#39;).addClass(&#39;hide&#39;);
  }
});

$(&#39;input[chck-type=&quot;c&quot;]&#39;).click(function(){
    if (
    $(this).prop(&quot;checked&quot;) == true
  ) {
      $(&#39;.C_Box&#39;).removeClass(&#39;hide&#39;);
  } else {
    $(&#39;.C_Box&#39;).addClass(&#39;hide&#39;);
  }
});

<!-- language: lang-css -->

.hide {display:none}

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;A&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot;&gt;B&lt;/p&gt;
&lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;c&quot;&gt;C&lt;/p&gt;


&lt;div class=&#39;A_B hide&#39;&gt;
This box is for A or B.
&lt;/div&gt;

&lt;div class=&#39;C_Box hide&#39;&gt;
This box is for C.
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

只需将您的JS更改为以下内容,就能正常工作。

$('input').on('click', function () {
  var a_b = $('input[chck-type="a_b"]').is(':checked');
  if (!a_b) {
    $('.A_B').addClass('hide');
  } else {
    $('.A_B').removeClass('hide');
  }

  var c = $('input[chck-type="c"]').is(':checked');
  if (!c) {
    $('.C_Box').addClass('hide');
  } else {
    $('.C_Box').removeClass('hide');
  }
});

您的HTML和CSS部分保持不变。

英文:

Simply change your JS to this and will work.

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

&lt;!-- language: lang-js --&gt;


    $(&#39;input&#39;).on(&#39;click&#39;, function () {
      var a_b = $(&#39;input[chck-type=&quot;a_b&quot;]&#39;).is(&#39;:checked&#39;);
      if (!a_b) {
        $(&#39;.A_B&#39;).addClass(&#39;hide&#39;);
      } else {
        $(&#39;.A_B&#39;).removeClass(&#39;hide&#39;);
      }

      var c = $(&#39;input[chck-type=&quot;c&quot;]&#39;).is(&#39;:checked&#39;);
      if (!c) {
        $(&#39;.C_Box&#39;).addClass(&#39;hide&#39;);
      } else {
        $(&#39;.C_Box&#39;).removeClass(&#39;hide&#39;);
      }
    });


&lt;!-- language: lang-css --&gt;

    .hide {
      display: none;
    }


&lt;!-- language: lang-html --&gt;

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
    &lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot; /&gt;A&lt;/p&gt;
    &lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;a_b&quot; /&gt;B&lt;/p&gt;
    &lt;p&gt;&lt;input type=&quot;checkbox&quot; chck-type=&quot;c&quot; /&gt;C&lt;/p&gt;

    &lt;div class=&quot;A_B hide&quot;&gt;This box is for A or B.&lt;/div&gt;

    &lt;div class=&quot;C_Box hide&quot;&gt;This box is for C.&lt;/div&gt;

<!-- end snippet -->

Example : Stackblitz

huangapple
  • 本文由 发表于 2023年2月6日 11:54:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357203.html
匿名

发表评论

匿名网友

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

确定