最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - hiding div based on unchecking checkboxes - Stack Overflow

programmeradmin7浏览0评论

I have multiple checkboxes in a form. Based on clicking those checkboxes, I show a div section. But if I uncheck even one checkbox, that div section gets hidden. How do I make sure that div section is hidden only if all checkboxes are unchecked. Crude way can be to write my own 'display' method which will check if all checkboxes are unchecked and then hide the div section. Any easier solution??

I have multiple checkboxes in a form. Based on clicking those checkboxes, I show a div section. But if I uncheck even one checkbox, that div section gets hidden. How do I make sure that div section is hidden only if all checkboxes are unchecked. Crude way can be to write my own 'display' method which will check if all checkboxes are unchecked and then hide the div section. Any easier solution??

Share Improve this question asked Jul 20, 2010 at 17:52 yogsmayogsma 10.6k31 gold badges106 silver badges157 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

HTML:

<input type="checkbox" class="group" name="check1">
<input type="checkbox" class="group" name="check2">
<input type="checkbox" class="group" name="check3">
<input type="checkbox" class="group" name="check4">

jQuery:

$(function() {
    var $checks = $('input:checkbox.group');
    $checks.click(function() {
        if($checks.filter(':checked').length == 0) {
            $('#div').hide();
        } else {
            $('#div').show();
        }
    });
});

The following code will show the div if one or more checkboxes has been checked:

jQuery

Version 1:

$("input[name='mycheckboxes']").change(function() {
  $("#showme").toggle($("input[name='mycheckboxes']:checked").length>0);
});

Version 2 (more efficient):

var MyCheckboxes=$("input[name='mycheckboxes']");

MyCheckboxes.change(function() {
  $("#showme").toggle(MyCheckboxes.is(":checked"));
});

HTML

<input type="checkbox" name="mycheckboxes" />
<input type="checkbox" name="mycheckboxes" />
<input type="checkbox" name="mycheckboxes" />
<input type="checkbox" name="mycheckboxes" />

<div id="showme" style="display: none">Show me</div>

Code in action (Version 1).

Code in action (Version 2).

--- Different Checkbox Names Version ---

For different named checkboxes, wrap them in a DIV with an identifier. E.g.

jQuery

var MyCheckboxes=$("#checkboxgroup :checkbox");

MyCheckboxes.change(function() {
  $("#showme").toggle(MyCheckboxes.is(":checked"));
});

HTML

<div id="checkboxgroup">
  <input type="checkbox" name="mycheckbox1" />
  <input type="checkbox" name="mycheckbox2" />
  <input type="checkbox" name="mycheckbox3" />
  <input type="checkbox" name="mycheckbox4" />
</div>

<div id="showme" style="display: none">Show me</div>

This code in action.

Not really, you need Javascript for this one... Or maybe... Let's say:

<html>
<head>
<style type="text/css">
  #input_container > input + input + input + div {display:none}
  #input_container > input:checked + input:checked + input:checked + div {display:block}
</style>
</head>
<div id="input_container">
  <input type="checkbox">blah1
  <input type="checkbox">blah2
  <input type="checkbox">blah3
  <div>To show/hide</div>
</div>
</body>
</html>

I'd create a function that uses a variable that tracks the number of checkboxes checked:

var numberOfChecks = 0;
function display(ev) {
  var e = ev||window.event;
  if (this.checked) {
    numberOfChecks++;
  } else {
    numberOfChecks--;
  }
  if (!numberOfChecks) {
    //hide div code
  } else {
    //display div code
  }
}

Use that function for each onClick event for every checkbox. In the ideal world this would be done inside some initialization function so that numberOfChecks and display aren't in the global namespace.

Plain Javascript:

HTML

<div id="checkboxes">
<input type="checkbox" name="check1">
<input type="checkbox" name="check2">
<input type="checkbox" name="check3">
<input type="checkbox" name="check4">
</div>

<div id="hiddendiv"><!-- more stuff --></div>

Javascript

(function() { //Create clousre to hide the checked variable
var checked = 0;
var inputs = document.getElementById('checkboxes').getElementsByTagName('input');
for (var i=0, l=inputs.length; i<l; i++) {
  if (inputs[i].type == 'checkbox') {
     if (inputs[i].checked) checked++; //Count checkboxes that might be checked on page load
     inputs[i].onchange = function() {
       checked += this.checked ? 1 : -1;

       var hiddendiv = document.getElementById('hiddendiv');
       if (!checked) hiddendiv.style.display = "none";
       else hiddendiv.style.display = "";
     };
  }
}
}());

The other option is to simply iterate through each checkbox every time the change event is fired rather than relying on counting, which is probably more error prone. Obviously jQuery is more concise, but a little verbosity never hurt anyone.

function toggleCheckbox(id) {
            if ($("input[id=" + id + "]").is(':checked')) {
                $( "#"+id ).prop( "checked", false );
            } else {
                $( "#"+id ).prop( "checked", true );
            }
        }

Just pass the id of your checkbox

发布评论

评论列表(0)

  1. 暂无评论