【问题标题】:Show a div if 2 specific checkboxes are checked, show another if 1 checkbox is checked (don't show it if the 2 checkboxes are checked)如果选中了 2 个特定的复选框,则显示一个 div,如果选中了 1 个复选框,则显示另一个(如果选中了 2 个复选框,则不显示)
【发布时间】:2014-09-14 21:06:47
【问题描述】:

我的代码中有红色、绿色和蓝色框作为 3 个不同的 div,我想根据选中的复选框显示这些 div。

目的是:如果选中蓝色复选框,则应显示蓝色 div。如果绿色 div 被选中,它应该显示绿色 div。但是,要显示红色 div,应该选中红色和绿色复选框。

这些在我的代码中工作,但有一个问题。如果选中红色和绿色复选框,则会出现红色 div,但也会出现绿色 div。

我需要您的帮助是:如果选中红色和绿色复选框,则应显示红色 div,但不应显示绿色复选框。 (如果仅选中绿色复选框,则应显示绿色 div。)

感谢您的帮助!

代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Using Checkboxes</title>
<style type="text/css">
.box{
    padding: 20px;
    display: none;
    margin-top: 20px;
    border: 1px solid #000;
}
.red{ background: #ff0000; }
.green{ background: #00ff00; }
.blue{ background: #0000ff; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){
        if($('#red').is(':checked') && $('#green').is(':checked'))
        {
            $(".red").show();}
            else{ $(".red").hide();
        }

        if($('#green').is(':checked'))
        {
            $(".green").show();}
            else{ $(".green").hide();
        }

        if($('#blue').is(':checked'))
        {
            $(".blue").show();}
            else{ $(".blue").hide();
        }

    });
});


</script>
</head>
<body>
<div>
    <label><input type="checkbox" id="red" name="colorCheckbox" value="red">        red</label>
    <label><input type="checkbox" id="green" name="colorCheckbox" value="green"> green</label>
    <label><input type="checkbox" id="blue" name="colorCheckbox" value="blue"> blue</label>
</div>
<div class="red box">You have selected <strong>red and green checkbox</strong> so i am here</div>
<div class="green box">You have selected <strong>green checkbox</strong> so i am here</div>
<div class="blue box">You have selected <strong>blue checkbox</strong> so i am here</div>
</body>
</html>

【问题讨论】:

  • 考虑一个单独的并完成组合条件(if-statement),以便每个 div 显示/隐藏。
  • 感谢您的帮助。

标签: javascript jquery html if-statement checkbox


【解决方案1】:

你问题的逻辑是正确的!

只需将这些要求转化为代码,一切就绪!

//If the blue checkbox is checked, it should show the blue div.
if($('#blue').is(':checked')) {
    $(".blue").show();
} else { 
    $(".blue").hide();
}
//If the green div is checked, it should show the green div. 
if($('#green').is(':checked')) {
    $(".green").show();
} else { 
    $(".green").hide();
}

//To show the red div, the red AND the green checkbox should be checked
//but not the green box.
if($('#red').is(':checked') && $('#green').is(':checked'))  {
    $(".red").show();
    $(".green").hide();
} else { 
    $(".red").hide();
    //no show green necessary, it will be handled above
}

【讨论】:

  • 当心 'Spagetti-Code' ;-) 考虑将“&& !checked(red)”添加到绿色 div 的条件中,而不是设置两次绿色状态...跨度>
  • 感谢您的帮助。
【解决方案2】:
$('input[type="checkbox"]').click(function () {
    $('.box').hide();
    if ($('#blue').is(':checked')) $('.blue.box').show();
    if ($('#green').is(':checked') && !$('#red').is(':checked')) $('.green.box').show();
    if ($('#red').is(':checked') && $('#green').is(':checked')) $('.red.box').show();
});

jsFiddle example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 2021-12-24
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多