我制作了一个复选框列表,当有人检查复选框时,我需要它将待处理文本更改为完成,并将背景更改为绿色。我已经设法使用jQuery完成所有工作。
除了它为每个复选框容器设置样式,而不仅仅是选中的容器。我该如何解决这个问题?如果有人在之后再次取消选中复选框,我怎样才能将其更改回原始样式?
到目前为止,这是我的代码:
$('.custom-control-input').click(function(){
if (this.checked) {
$('.pending').css('display', 'none')
$('.done').css('display', 'block')
$('.list-group-item').css('background-color','#4CAF50')
}
})

.done {
display: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
</ul>
&#13;
答案 0 :(得分:5)
添加$('.custom-control-input').click(function(){
$(this).closest('.list-group-item').find('.pending').css('display', (this.checked)?'none':'block')
$(this).closest('.list-group-item').find('.done').css('display', (this.checked)?'block':'none')
$(this).closest('.list-group-item').css('background-color',(this.checked)?'#4CAF50':'')
})
以更改checkedcheckbox唯一父级的背景
.done {
display: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
</ul>
&#13;
Runtime Error
Cannot read property 'lat' of undefined
Stack
TypeError: Cannot read property 'lat' of undefined
at HomePage.loadMap (http://localhost:8100/build/main.js:58172:54)
at HomePage.ionViewDidLoad (http://localhost:8100/build/main.js:58164:14)
at ViewController._lifecycle (http://localhost:8100/build/main.js:17293:33)
at ViewController._didLoad (http://localhost:8100/build/main.js:17166:14)
at NavControllerBase._didLoad (http://localhost:8100/build/main.js:43860:18)
at t.invoke (http://localhost:8100/build/polyfills.js:3:8971)
at Object.onInvoke (http://localhost:8100/build/main.js:4480:37)
at t.invoke (http://localhost:8100/build/polyfills.js:3:8911)
at r.run (http://localhost:8100/build/polyfills.js:3:4140)
at NgZone.run (http://localhost:8100/build/main.js:4348:62)
&#13;
答案 1 :(得分:0)
此脚本将修复
$('.custom-control-input').click(function(){
$(this).parents('.list-group-item').css('background-color', '#fdb515');
$(this).parents('.list-group-item').find('.mb-0.done').show();
$(this).parents('.list-group-item').find('.mb-0.pending').hide();
})
.mb-0.done{
display:none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
<li class="list-group-item">
<div class="form-check">
<label class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description ml-4">Checkbox</span>
</label>
</div> <!-- /.form-check -->
<div class="ml-auto status">
<p class="mb-0 done">done</p>
<p class="mb-0 pending">pending</p>
</div>
</li>
</ul>