开发者

Show hide div based on multiple checkboxes

开发者 https://www.devze.com 2023-02-13 15:12 出处:网络
I have a div (\"#CME\") that is hidden initially, I need to show it if ALL 6 of these checkboxes are checked.

I have a div ("#CME") that is hidden initially, I need to show it if ALL 6 of these checkboxes are checked.

$("#CME").hide();
    (function() {
        if("#CME1") && ("#CMEQL") && ("#CBT1") && ("#CBTQL") && ("#NYM1") && ("#CMX1").is(":checked") {
            $("#CME").show();
        } el开发者_JAVA百科se {
            $("#CME").hide();
        }
    });

This keeps getting an error in firebug. thx.


Couple of issues with your code:

1) Syntax of if is wrong.

2) jQuery object prefix(jQuery/$) is missing in the if condition.

3) is function of jQuery return true even if one element in the selection is satisfiying the is condition which would yield wrong result as per your requirement.

Try this:

 $("#CME").hide();
$(function() {
        $("#CME1, #CMEQL, #CBT1, #CBTQL, #NYM1, #CMX1").change(function(){
        var checkBoxes =  $("#CME1, #CMEQL, #CBT1, #CBTQL, #NYM1, #CMX1").filter(":not(:checked)");
            if(checkBoxes.length == 0){
                    $("#CME").show();
            } else {
                    $("#CME").hide();
            }
    });
});

Working example @ : http://jsfiddle.net/t5qZ7/5/

0

精彩评论

暂无评论...
验证码 换一张
取 消