В следующем примере показано, как отображать и скрывать элементы <div>
на основе выбора чекбоксов с помощью jQuery-метода toggle()
. Блоки <div>
в примере по умолчанию скрыты с помощью CSS-свойства display
, для которого установлено значение none
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Checkboxes</title>
<style>
.box{
color: #fff;
padding: 20px;
display: none;
margin-top: 20px;
}
.red{ background: #ff0000; }
.green{ background: #228B22; }
.blue{ background: #0000ff; }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
var inputValue = $(this).attr("value");
$("." + inputValue).toggle();
});
});
</script>
</head>
<body>
<div>
<label><input type="checkbox" name="colorCheckbox" value="red"> Красный</label>
<label><input type="checkbox" name="colorCheckbox" value="green"> Зеленый</label>
<label><input type="checkbox" name="colorCheckbox" value="blue"> Синий</label>
</div>
<div class="red box">Вы установили <strong>красный чекбокс</strong></div>
<div class="green box">Вы установили <strong>зеленый чекбокс</strong></div>
<div class="blue box">Вы установили <strong>синий чекбокс</strong</div>
</body>
</html>