gpt4 book ai didi

javascript - 使用复选框显示/隐藏元素

转载 作者:行者123 更新时间:2023-11-28 17:33:17 27 4
gpt4 key购买 nike

在我的代码片段中,我想单击单击我以使文本消失,并出现一个复选框并被选中。这部分有效。

我无法工作的是取消选中该复选框应该使该复选框隐藏并且click me文本重新出现,但该复选框根本不会取消选中。

function doStuff() { 
if ($('#thecb').prop('checked') == true) {
hideDiv('thecb');
showDiv('thediv');
} else {
hideDiv('thediv');
showDiv('thecb');
$('#thecb').prop('checked',true);
}
}

function showDiv(id) {
document.getElementById(id).style.display = "block";
}

function hideDiv(id) {
document.getElementById(id).style.display = "none";
}
.hidden {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="thediv" onclick="doStuff()">click me</div>
<input type="checkbox" id="thecb" onclick="doStuff()" class="hidden">

最佳答案

您将立即使用 $('#thecb').prop('checked',true); 重新选中该复选框。将其放在其他部分中:

function doStuff() { 
if (!$('#thecb').prop('checked')) {
hideDiv('thecb');
showDiv('thediv');
$('#thecb').prop('checked', true);
} else {
hideDiv('thediv');
showDiv('thecb');
}
}

function showDiv(id) {
document.getElementById(id).style.display = "block";
}

function hideDiv(id) {
document.getElementById(id).style.display = "none";
}
.hidden {
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="thediv" onclick="doStuff()">click me</div>
<input type="checkbox" checked id="thecb" onclick="doStuff()" class="hidden">

关于javascript - 使用复选框显示/隐藏元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com