我有一个基本的if / else语句,检查是否检查了两个输入,如果是,则第三个复选框状态为"禁用"变为假,可以检查。我希望一旦检查过,如果取消选中其他复选框,则会再次取消选中此复选框,但它会在检查时被禁用。
我的代码:
$("#cvLinkedInAssessmentCheck :input, #rolePlayingTalentCheck :input").change(function(){
if( $(this).is(':checked') ){
$("#minutosTalentoCheck :input").attr("disabled", false);
$("#minutosTalentoCheck").css("opacity", "1");
} else {
$("#minutosTalentoCheck").prop('checked', false);
$("#minutosTalentoCheck :input").attr("disabled", true);
$("#minutosTalentoCheck").css("opacity", "0.3");
}
})
基本上,else语句中的这一行不起作用,我也试过" attr"相反没有运气:
$("#minutosTalentoCheck").prop('checked', false);
答案 0 :(得分:1)
var $things = $("#cvLinkedInAssessmentCheck :input, #rolePlayingTalentCheck :input");
$things.on('change', function() {
var $otherThing = $("#minutosTalentoCheck");
//if either of the things are not checked, disable the other thing
if ($things.not(':checked').length) {
$otherThing
.prop('checked', false)
.attr("disabled", true)
.css("opacity", "0.3");
} else {
$otherThing
.prop("disabled", false)
.css("opacity", "1");
}
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="cvLinkedInAssessmentCheck">
<input type="checkbox"> Assessment
</div>
<div id="rolePlayingTalentCheck">
<input type="checkbox"> Talent
</div>
<input type="checkbox" id="minutosTalentoCheck" disabled> Other thing
&#13;