如何制作一个取消选中所有复选框并更改div背景色的按钮



我有以下代码:jiddle.com

div中有两个带有图片和标签的复选框。选中复选框后,div将背景颜色更改为橙色。

现在我添加了一个按钮,可以取消选中所有复选框,但背景颜色不会变回白色。现在有人知道我的错在哪里吗?

function uncheckAll() {
$("input[type='checkbox']:checked").prop("checked", false)
}
$('#aufheben_button').on('click', uncheckAll)

$("input[type='checkbox']").change(function(){
if($(this).is(":checked", true)){
$(this).parent().addClass("changeBackground"); 
}
else{
$(this).parent().removeClass("changeBackground");
$('#aufheben_button').parent().removeClass("changeBackground");  
}
});
li{
float: left;
width: 30%;
min-width: 160px;
height: 130px;
margin: 0 0 3px 0;
font-size: 10px;
line-height: 1.4;
text-align: center;
background-color: #f9f9f9;
border: 5px solid #fff;
position: relative;
cursor: pointer;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
user-select: none;
list-style: none;
display: block;
}
.app_name{

font-size: calc(10px + 0.4vw);
}
label{
display: block;
padding-right: 25px;
}
.picture{
position: relative;
display: block;
margin: 0 auto;
width: 100px;
height: 100px 
}
/* Customize the label (the container) */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}

/* Hide the browser's default checkbox */
.container input {
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}

/* Create a custom checkbox */
.checkmark {
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #f9f9f9;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark  {
background-color: #FD9B08;
}
/* Ändern des Hintergrunds bei der Groupbox */
.isChecked {
background-color: #FD9B08;
}

/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
.changeBackground{
background-color: #FD9B08;
}

.div_groupbox{
background-color: #f9f9f9;
width: auto;
}
.div_groupbox:hover{
background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><html>
<head>
</head>
<body>
<button type="button" class="aufheben" id="aufheben_button">unselect checkmarks</button>

<ul>
<li class="background_li">
<div class="div_groupbox">               
<label class="container">
<img id="Firefox" src="Bilder/Test1.png" class="picture"> 
<div class="app_name">Test1</div><input type="checkbox" name="acs" value="Test1"> 
<span class="checkmark"></span>
</label>                         
</div>
</li>
<li>
<div class="div_groupbox">
<label class="container">
<img src="Bilder/Test2.png" class="picture">
<div class="app_name">Test2</div><input type="checkbox" name="acs" value="Test2">
<span class="checkmark"></span>
</label>
</div>
</li>
</ul>
</body>
</html>

您需要将其添加到uncheckAll函数中:

$("input[type='checkbox']:checked").prop("checked", false);
$("input[type='checkbox']").parent().removeClass("changeBackground");

并在按钮中添加一个onclick="uncheckAll()"

更改事件处理程序将不会使用以下代码执行:

$("input[type='checkbox']:checked").prop("checked", false);

如果你想执行变更事件处理程序中的任何内容,那么触发点击事件,点击事件将切换复选框并执行由此产生的任何事件处理程序:

$("input[type='checkbox']:checked").trigger("click");

最新更新