简体   繁体   English

根据单选按钮单击显示/隐藏表单字段

[英]Show/hide form fields depending on radio button click

So I have a radio button that when checked, I would like the below two fields shown/hidden.所以我有一个单选按钮,当检查时,我希望下面的两个字段显示/隐藏。 How would this be achievable via JS?这将如何通过 JS 实现?

<div class="custom-control custom-switch mt-3">
    <input type="checkbox" class="custom-control-input" id="rtk1" name="rtk[checked][]" value="1">
    <label class="custom-control-label" for="rtk1">The categories of personal information <?= $config->get_company(); ?> has collected about you.</label>
</div>
<div class="row mt-3">
    <div class="col-sm-1"></div>
    <div class="col-sm-11">
        <i>Please provide the following information to be used for verification purposes:</i>
    </div>
</div>
<div class="row mt-3">
    <div class="col-sm-1"></div>
    <label for="rtk1Field1" class="col-sm-2 col-form-label">Field 1</label>
    <div class="col-sm-9">
        <input type="text" class="form-control" id="rtk1Field1" name="rtk[1][field1]" required>
        <div class="invalid-feedback">
            Field 1 is required.
        </div>
    </div>
</div>
<div class="row mt-3">
    <div class="col-sm-1"></div>
    <label for="rtk1Field2" class="col-sm-2 col-form-label">Field 2</label>
    <div class="col-sm-9">
        <input type="text" class="form-control" id="rtk1Field2" name="rtk[1][field2]" required>
        <div class="invalid-feedback">
            Field 2 is required.
        </div>
    </div>
</div>

Working demo: https://jsfiddle.net/prceax3v/3/工作演示: https : //jsfiddle.net/prceax3v/3/

HTML HTML

<div class="custom-control custom-switch mt-3">
  <input type="checkbox" class="custom-control-input" id="rtk1" name="rtk[checked][]" value="1">
  <label class="custom-control-label" for="rtk1">The categories of personal information <?= $config->get_company(); ?> has collected about you.</label>
</div>
<div class="row mt-3">
  <div class="col-sm-1"></div>
  <div class="col-sm-11">
    <i>Please provide the following information to be used for verification purposes:</i>
  </div>
</div>
<div id='form-wrapper'>


  <div class="row mt-3">
    <div class="col-sm-1"></div>
    <label for="rtk1Field1" class="col-sm-2 col-form-label">Field 1</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="rtk1Field1" name="rtk[1][field1]" required>
      <div class="invalid-feedback">
        Field 1 is required.
      </div>
    </div>
  </div>
  <div class="row mt-3">
    <div class="col-sm-1"></div>
    <label for="rtk1Field2" class="col-sm-2 col-form-label">Field 2</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="rtk1Field2" name="rtk[1][field2]" required>
      <div class="invalid-feedback">
        Field 2 is required.
      </div>
    </div>
  </div>
</div>

JS JS

const formWrapper = document.getElementById('form-wrapper');

document.getElementById('rtk1').addEventListener('change', (e) => {
  if (e.target.checked) {
    formWrapper.style.display = 'none'
  } else {
    formWrapper.style.display = ''
  }
})

First, what you've provided is a checkbox, not a radio button.首先,您提供的是一个复选框,而不是一个单选按钮。 If you want to use a checkbox, you can accomplish what you're asking with the following code:如果您想使用复选框,您可以使用以下代码完成您的要求:

document.querySelector('input#rtk1').addEventListener( 'change', function() {
if(this.checked) {
    document.querySelectorAll('input[type="text"]').forEach(function(el) {
      el.style.display = 'none';
    });
} else {
  document.querySelectorAll('input[type="text"]').forEach(function(el) {
    el.style.display = 'block';
  });
}
});

If you want to hide the labels next to each text field as well, I would suggest adding a unique class to each row container such as, text-input-container , then putting that class name inside the querySelectorAll calls instead of input[type="text"]如果您还想隐藏每个文本字段旁边的标签,我建议向每个行容器添加一个唯一的类,例如text-input-container ,然后将该类名放在 querySelectorAll 调用中,而不是input[type="text"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM