【问题标题】:How to hide radio buttons in JavaScript on button click [duplicate]如何在按钮单击时隐藏 JavaScript 中的单选按钮 [重复]
【发布时间】:2011-11-03 10:56:37
【问题描述】:

可能重复:
javascript hide/show element

如何隐藏单选按钮,单选按钮应仅在用户单击显示按钮时显示 请您提供示例程序或为此建议网站 提前谢谢你

【问题讨论】:

标签: javascript html


【解决方案1】:

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 中选择容器,并将其存储在一个变量中以提高效率和缩短代码。我将显示设置为无(隐藏),然后将事件处理程序附加到 id 为“theButton”(不在 html 中)的元素的单击事件以再次显示。

注意:我用 Javascript 隐藏它们,否则表单可能对不使用 javascript(它们确实存在!)的人无法使用,或者会出现 javascript 错误。

【讨论】:

    【解决方案2】:

    将单选按钮放在具有样式 display:none 的 div 中,并在提交按钮上 Onclick 事件使用 javascript 调用一个函数,并在该函数中将 display 设置为 div 的块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多