最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ShowHide DIV by Multiple Checkboxes - Stack Overflow

matteradmin4PV0评论

I am trying to show/hide a form inside a div, conditional on multiple checkboxes. If all 3 are checked, the div contents should appear. If one or more are unchecked, the div contents should be hidden.

<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="style/bootstrap.css" rel="stylesheet">
        <script src="js/jquery.js"></script>
        <script>
            if ($('#checkbox1, #checkbox2, #checkbox3').is(:checked)) {
               $('#purchaseForm').show();
            } else {
               $('#purchaseForm').hide();
            }
        </script>
    </head>
    <body>
        <div class="span6">
            I understand that...<br>
            I understand that...<br>
            I understand that...<br><br>

        <div id="purchaseForm">
            [form]
        </div>          
        </div>

        <div class="span6">
            <input name="checkbox1" id="checkbox1" value="" type="checkbox"><br>
            <input name="checkbox2" id="checkbox2" value="" type="checkbox"><br>
            <input name="checkbox3" id="checkbox3" value="" type="checkbox"><br>
        </div>
    </body>
</html>

I am trying to show/hide a form inside a div, conditional on multiple checkboxes. If all 3 are checked, the div contents should appear. If one or more are unchecked, the div contents should be hidden.

<!DOCTYPE html>
<html lang="en">
    <head>
        <link href="style/bootstrap.css" rel="stylesheet">
        <script src="js/jquery.js"></script>
        <script>
            if ($('#checkbox1, #checkbox2, #checkbox3').is(:checked)) {
               $('#purchaseForm').show();
            } else {
               $('#purchaseForm').hide();
            }
        </script>
    </head>
    <body>
        <div class="span6">
            I understand that...<br>
            I understand that...<br>
            I understand that...<br><br>

        <div id="purchaseForm">
            [form]
        </div>          
        </div>

        <div class="span6">
            <input name="checkbox1" id="checkbox1" value="" type="checkbox"><br>
            <input name="checkbox2" id="checkbox2" value="" type="checkbox"><br>
            <input name="checkbox3" id="checkbox3" value="" type="checkbox"><br>
        </div>
    </body>
</html>
Share Improve this question asked Aug 29, 2013 at 2:52 user2449026user2449026 4572 gold badges7 silver badges9 bronze badges 1
  • 2 is(:checked) is this how your code is? that should be is(':checked') – Ohgodwhy Commented Aug 29, 2013 at 2:54
Add a ment  | 

5 Answers 5

Reset to default 1

You are currently checking to see if any of the checkboxes are checked. Make sure they're all checked.

if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked') && $('#checkbox3').is(':checked'))

DEMO

$(function(){  
  var $cbx  = $(':checkbox[id^=check]'),
      $form = $('#purchaseForm');  
  $cbx.change(function(){
    $form[$cbx.not(':checked')[0]?'hide':'show']();
  }).change();
});

You can listen to the change event of checkboxes DEMO Here:

$(function(){
var container = $('#container');
var purchaseForm = $('#purchaseForm');
    container.on('change', 'input[type="checkbox"]', function(){
        if(container.find('input[type="checkbox"]').not(':checked').length){
        purchaseForm.hide();
        }
        else{
        purchaseForm.show();
        }
    });
    container.find('input[type="checkbox"]:first').change();
});

Try

$(document).ready(function() {
var a = $("#checkbox1");
var b = $("#checkbox2");
var c = $("#checkbox3");
var bined = a.add(b).add(c);
$(bined).click(function() //anyone of bined cliecked event
{
    // if ($('#checkbox1, #checkbox2, #checkbox3').is(':checked'))    this means         one of those is checked
    if ($(a).is(':checked') && $(b).is(':checked') && $(c).is(':checked')) {
        $('#purchaseForm').show();
    } else {
        $('#purchaseForm').hide();
    }
});

});

Try

jQuery(function(){
    var checks = $('#checkbox1, #checkbox2, #checkbox3');

    checks.click(function(){
        if (checks.filter(':checked').length == checks.length) {
            $('#purchaseForm').show();
        } else {
            $('#purchaseForm').hide();
        }
    }).triggerHandler('click')


})

Demo: Fiddle

Post a comment

comment list (0)

  1. No comments so far