1

我正在尝试在 div 中显示/隐藏表单,以多个复选框为条件。如果所有 3 个都被选中,则应该出现 div 内容。如果一个或多个未选中,则应该隐藏 div 内容。

<!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>
4

5 回答 5

1

Try

$(document).ready(function() {
var a = $("#checkbox1");
var b = $("#checkbox2");
var c = $("#checkbox3");
var combined = a.add(b).add(c);
$(combined).click(function() //anyone of combined 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();
    }
});

});
于 2013-08-29T03:43:29.270 回答
1

您可以在此处收听复选框DEMO的更改事件:

$(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();
});
于 2013-08-29T03:01:32.970 回答
1

演示

$(function(){  
  var $cbx  = $(':checkbox[id^=check]'),
      $form = $('#purchaseForm');  
  $cbx.change(function(){
    $form[$cbx.not(':checked')[0]?'hide':'show']();
  }).change();
});
于 2013-08-29T03:12:43.437 回答
1

您当前正在检查是否选中了任何复选框。确保它们都已检查。

if ($('#checkbox1').is(':checked') && $('#checkbox2').is(':checked') && $('#checkbox3').is(':checked'))
于 2013-08-29T03:00:40.997 回答
0

尝试

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

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


})

演示:小提琴

于 2013-08-29T02:59:46.943 回答