Show hide div based on multiple checkboxes
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/
精彩评论