简体   繁体   中英

click multiple elements to hide or show something - jquery

here is my script. Now i can click one of these IDs and class "inputs" are visible. What i want is that I have to click on all elements.

$('#zwei,#sechs,#neun').bind('click', function() {
    if( $(this).is(':checked')) {
        $('.inputs').show();
    } else {
        $('.inputs').hide();
    }
}); 

JSFiddle: http://jsfiddle.net/CLYC6/20/

can you help me please? whats wrong?

FK

Use this:

$('#zwei,#sechs,#neun').bind('click', function() {
    $('.inputs').show();
    $('#zwei,#sechs,#neun').each(function (e) {
        if (!$(this).is(':checked')) {
            $('.inputs').hide();
            return;
        }
    });
}); 

Here is a LIVE DEMO .

Because @Rastko is not happy with the current solution here is one more:

$('#zwei,#sechs,#neun').bind('click', function() {
    var showInput = true;
    $('#zwei,#sechs,#neun').each(function (e) {
        if (!$(this).is(':checked')) {
            showInput = false;
            return;
        }
    });
    if (showInput) {
        $('.inputs').show();
    } else {
        $('.inputs').hide();
    }
}); 

One more LIVE DEMO .

If statement should check whether all three are checked, and if input is not visible.

so:

if($('#zvei').is(':checked') && $('#neun').is(':checked') && $('#sechs').is(':checked') {
      $('.inputs').show();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM