嗨,我需要代码方面的帮助,所以基本上我正在做一个项目,当你选中复选框时,它会显示一个文本结果。我可以创建一个if-else条件,但我遇到了一个问题,如果选中了所有复选框,它将显示所有文本。
这是我的代码
<!DOCTYPE html>
<html>
<body>
<p>Display some text when the checkbox is checked:</p>
<label for="myCheck">Checkbox:</label>
<input type="checkbox" id="myCheck" onclick="myFunction()">
<label for="myCheck">Checkbox2:</label>
<input type="checkbox" id="myCheck1" onclick="myFunction()">
<p id="text" style="display:none"> both</p>
<p id="text2" style="display:none">ch1!</p>
<p id="text3" style="display:none">ch2!</p>
<script>
function myFunction() {
var checkBox = document.getElementById("myCheck");
/*should only show text "both"*/
if (checkBox.checked == true && myCheck1.checked == true){
text.style.display = "block";
} else {
text.style.display = "none";
}
/* show text "ch1"*/
if (myCheck.checked == true){
text2.style.display = "block";
} else {
text2.style.display = "none";
}
/* show text "ch2"*/
if (myCheck1.checked == true){
text3.style.display = "block";
} else {
text3.style.display = "none";
}
}
</script>
</body>
</html>
我试着用这样的方式隐藏其他文本
if (checkBox.checked == true && myCheck1.checked == true){
text.style.display = "block";
text2.style.display = "none";
text3.style.display = "none";
} else {
text.style.display = "none";
但还是没用,我真的需要帮助,谢谢。