可以通过以下步骤完成:
以下是一个示例代码:
<!DOCTYPE html>
<html>
<head>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<input type="radio" name="visibility" value="show" id="showRadio">
<label for="showRadio">显示文本输入</label>
<input type="radio" name="visibility" value="hide" id="hideRadio">
<label for="hideRadio">隐藏文本输入</label>
<br>
<input type="text" id="textInput" class="hidden">
<script>
const showRadio = document.getElementById("showRadio");
const hideRadio = document.getElementById("hideRadio");
const textInput = document.getElementById("textInput");
showRadio.addEventListener("click", function() {
textInput.classList.remove("hidden");
});
hideRadio.addEventListener("click", function() {
textInput.classList.add("hidden");
});
</script>
</body>
</html>
在这个示例中,当选中"显示文本输入"单选按钮时,文本输入框将显示出来;当选中"隐藏文本输入"单选按钮时,文本输入框将被隐藏起来。通过添加和移除名为"hidden"的CSS类来控制文本输入框的显示和隐藏。
领取专属 10元无门槛券
手把手带您无忧上云