如何在单击按钮时在JavaScript中隐藏单选按钮

How to hide radio buttons in JavaScript on button click

本文关键字:隐藏 JavaScript 单选按钮 按钮 单击      更新时间:2023-09-26

可能重复:
javascript隐藏/显示元素

如何隐藏单选按钮,单选按钮应仅在用户单击显示按钮时显示plz你能给我一个示例程序或建议一下这个网站吗并提前感谢

html:

<div id="radioButtonContainer">
    <input type="radio" name="selector" value="option1" id="selector1" /><label for="selector1">Option 1</label><br />
    <input type="radio" name="selector" value="option2" id="selector2" /><label for="selector2">Option 2</label><br />
    <input type="radio" name="selector" value="option3" id="selector3" /><label for="selector3">Option 3</label><br />
</div>

Javascript:

var radioButtonContainer = document.getElementById('radioButtonContainer');
radioButtonContainer.style.display = 'none';
document.getElementById('theButton').onclick = function() {
    radioButtonContainer.style.display = 'block';
};

说明:为了方便使用,我把按钮包在一个容器里。然后,我用javascript选择容器,并将其存储在一个变量中,以提高效率并缩短代码。我将显示设置为none(隐藏它(,然后将一个事件处理程序附加到id为"theButton"的元素(不是html(的点击事件,以再次显示它。

注意:我用Javascript隐藏它们,因为否则表单可能不适用于不使用Javascript的人(它们确实存在!(,或者当出现Javascript错误时。

将单选按钮放在一个样式为display:none的div中,并在提交按钮上使用javascript调用一个函数并在该函数中将显示为div的块。