2 回答

TA贡献1878条经验 获得超4个赞
您的要求有点不清楚,但我认为您不需要 switch 语句来实现功能,请参阅代码段。
如果您的“水果”复选框列表是动态的,您可以在ToggleFruitSelection函数中构建它
function ToggleFruitSelection(){
if(document.querySelector('input[name="formtype"]:checked').value === "custom"){
document.getElementById('customFruit').classList.remove("hidden");
}else{
document.getElementById('customFruit').classList.add("hidden");
}
}
.hidden{
display:none;
}
<label><input onChange=ToggleFruitSelection() value="standard" type="radio" name="formtype">Standard</label><br/>
<label><input onChange=ToggleFruitSelection() value="custom" type="radio" name="formtype">Build your own</label><br/>
<hr/>
<div class="hidden" id="customFruit">
<label><input value="standard" type="checkbox">Grape</label><br/>
<label><input value="standard" type="checkbox">Apple</label><br/>
<label><input value="standard" type="checkbox">Pear</label><br/>
<label><input value="standard" type="checkbox">Orange</label><br/>
<label><input value="standard" type="checkbox">Strawberry</label><br/>
<label><input value="standard" type="checkbox">Mango</label><br/>
<label><input value="standard" type="checkbox">Watermelon</label><br/>
</div>

TA贡献1817条经验 获得超14个赞
如果要求如下
1) 必须只有一个名为 Build your own 的单选按钮
2)如果选中单选按钮,则需要出现六个复选框
然后您可以使用 Jquery 尝试以下代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery Show Hide Elements Using Radio Buttons</title>
<style>
.custom{
display: none;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$('input[type="radio"]').click(function(){
var inputValue = $(this).attr("value");
var targetBox = $("." + inputValue);
$(".custom").not(targetBox).hide();
$(targetBox).show();
});
});
</script>
</head>
<body>
<div>
<label><input type="radio" name="colorRadio" value="custom"> Build your own</label>
</div>
<div class="custom" id="customFruit">
<label><input value="standard" type="checkbox">Grape</label><br/>
<label><input value="standard" type="checkbox">Apple</label><br/>
<label><input value="standard" type="checkbox">Pear</label><br/>
<label><input value="standard" type="checkbox">Orange</label><br/>
<label><input value="standard" type="checkbox">Strawberry</label><br/>
<label><input value="standard" type="checkbox">Mango</label><br/>
<label><input value="standard" type="checkbox">Watermelon</label><br/>
</div>
</body>
</html>
添加回答
举报