我试图显示/隐藏一个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>

最佳答案

尝试

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

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


})


演示:Fiddle

07-24 09:50
查看更多