【发布时间】:2019-02-04 11:31:36
【问题描述】:
我有一个复选框,在复选框下方有一个 div 区域,我想在其中显示一个下拉菜单和另一个复选框。这个区域我只想在第一个复选框被选中时显示。
我已经用 style.display 尝试过了,它成功了一半。当我选中复选框时,我可以显示第二个区域,但如果我取消选中它不会再次隐藏。如果我尝试使用 jquery 也是一样(同时发布)
//function F2
function functpe() {
var tpe = document.getElementById("tpe");
if (tpe.checked == true) {
document.getElementById("doing").style.display = "block";
} else {
document.getElementById("doing").style.display = "none";
}
}
function functpe() {
var tpe = $("#doing");
if (tpe.checked == true) {
$("#doing").css({
"display": "block"
});
} else {
$("#doing").css({
"display": "block"
});
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-check form-check-inline" style="font-size:20px">
<input class="form-check-input big-checkbox" type="checkbox" id="tpe" value="tpe" onchange="functpe()">
<label class="form-check-label" for="tpe">tpe</label>
</div>
<div id="doing" style="display:none">
<div class="form-group">
<label for="exampleFormControlSelect1">Example select</label>
<select class="form-control" id="tpe" style="width: 170px;">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-check form-check-inline" style="font-size:20px">
<input class="form-check-input big-checkbox" type="checkbox" id="n1" value="n1">
<label class="form-check-label" for="n1">n1</label>
</div>
<div class="form-check form-check-inline" style="font-size:20px">
<input class="form-check-input big-checkbox" type="checkbox" id="n2" value="n2" checked>
<label class="form-check-label" for="n2">n2</label>
</div>
<div class="form-check form-check-inline" style="font-size:20px">
<input class="form-check-input big-checkbox" type="checkbox" id="n3" value="n3" checked>
<label class="form-check-label" for="n3">n3</label>
</div>
</div>
我希望有人对我有想法
【问题讨论】:
-
当你标记这个 jquery 时:
$("#doing").show(tpe.checked); -
你是如何触发
functpe()的?你检查过tpe.checked(控制台/警报/调试步骤)的值吗?总是true? -
我在编辑中找到了您的代码 - 只需将第一行缩进 4 个或更多空格。
标签: javascript jquery html css