我希望我的if只有在两个条件得到验证时激活。我尝试了这个,但无法弄清楚如何做到这一点......
if (document.getElementById('SSD-250G'&&'HDD-1T').checked){
$('#ssd-check').css({'background-color':'green'})
}
如果你想要纯JS,你必须做两次:
if (document.getElementById('SSD-250G').checked && document.getElementById('HDD-1T').checked){
$('#ssd-check').css({'background-color':'green'})
}
if (document.getElementById('SSD-250G').checked && document.getElementById('HDD-1T').checked){
$('#ssd-check').css({'background-color':'green'})
}
#ssd-check {
height: 100px;
width: 100px;
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="SSD-250G" checked />
<input type="checkbox" id="HDD-1T" checked />
<div id="ssd-check">
</div>
或者,使用jQuery:
if ($('#SSD-250G').is(':checked') && $('#HDD-1T').is(':checked')){
$('#ssd-check').css({'background-color':'green'})
}
if ($('#SSD-250G').is(':checked') && $('#HDD-1T').is(':checked')){
$('#ssd-check').css({'background-color':'green'})
}
#ssd-check {
height: 100px;
width: 100px;
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="SSD-250G" checked />
<input type="checkbox" id="HDD-1T" checked />
<div id="ssd-check">
</div>
如果您使用的是jQuery,可以执行以下操作:
if(jQuery('#SSD-250G').prop('checked') && jQuery('#HDD-1T').prop('checked')){
jQuery('#ssd-check').css({'background-color':'green'})
}