单击时,单选按钮不执行JavaScript操作

时间:2018-07-28 00:14:18

标签: javascript html checkbox radio-button boolean

我正在尝试创建一个单选按钮,以打开另一个复选框。当我运行代码时,该复选框已经打开。我该怎么解决?

<script>
  function accountFunction() {
    if (document.getElementById('yesSrm').checked) {
      document.getElementById('acct').style.display = "inline";
    } else {
      document.getElementById('acct').style.display = "none";
    }
</script>
<form>
  <input id="yesSrm" name="yesSrm" type="radio" value="yes" onchange="accountFunction()" />SRM<br/>
  <br/>
  <div id="acct">
    <label for="account">Account</label>
    <input type="checkbox" name="account" id="account"><br/>
  </div>
</form>

How it appears

1 个答案:

答案 0 :(得分:0)

只需将display:none添加到acct块以默认将其隐藏

<script>
  function accountFunction() {
    if (document.getElementById('yesSrm').checked) {
      document.getElementById('acct').style.display = "block";
    } else {
      document.getElementById('acct').style.display = "none";
    }
  }
</script>
<form>
  <input id="yesSrm" name="yesSrm" type="radio" value="yes" onchange="accountFunction()" />SRM<br/>
  <br/>
  <div id="acct" style="display: none">
    <label for="account">Account</label>
    <input type="checkbox" name="account" id="account"><br/>
  </div>
</form>