Show element on radio button selection

2020-03-26 18:11发布

I would like to show input text field on radio button selection in vanilla JavaScript.

What am I missing?

const form = document.querySelector("form");
const size = form.elements["size"];
const total = form.elements["total"];

total.style.display = "none";

for (var i = 0; i < size.length; i++) {
  if (size[i].checked) {
    total.style.setProperty("display", "inherit");
  } else {
    total.style.setProperty("display", "none");
  }
}
<form>
<fieldset>
  <legend>Choose Size</legend>
  <label><input type="radio" name="size" id="six">6-inch</label>
  <label><input type="radio" name="size" id="eight">8-inch</label>
  <label><input type="radio" name="size" id="twelve">12-inch</label>
</fieldset>

<input type="text" name="total" readonly>
</form>

I’m aware that there are similar questions previously asked on Stack Overflow, but all of them are in jQuery.

3条回答
劳资没心,怎么记你
2楼-- · 2020-03-26 18:11

You need to add an event listener for the click event of each of the radio buttons.

<form>
<fieldset>
  <legend>Choose Size</legend>
  <label><input type="radio" name="size" id="six" required>6-inch</label>
  <label><input type="radio" name="size" id="eight">8-inch</label>
  <label><input type="radio" name="size" id="twelve">12-inch</label>
</fieldset>

<input type="text" name="total" readonly>
</form>
<script>
const form = document.querySelector("form");
const total = form.elements["total"];
const radios = form.querySelectorAll("input[name=size]");
total.style.display = "none";
radios.forEach(i=>{
i.addEventListener('click', function(e){
if(this.checked){
  total.style.setProperty("display", "inherit");
  total.value = this.id;//sets value of readonly input to the id of the selected radio button
} 
});
});
</script>

查看更多
在下西门庆
3楼-- · 2020-03-26 18:13

const form = document.querySelector("form");
const size = form.elements.size;
const total = form.elements.total;

total.style.display = "none";

for (var i = 0; i < size.length; i++) {
  size[i].onclick = function() {
    if (this.checked) {
      total.style.setProperty("display", "inherit");
      total.value = this.value;
    } else {
      total.style.setProperty("display", "none");
    }
  }
}
<form>
  <fieldset>
    <legend>Choose Size</legend>
    <label><input type="radio" name="size" value="6" id="six" required>6-inch</label>
    <label><input type="radio" name="size" value="8" id="eight">8-inch</label>
    <label><input type="radio" name="size" value="12" id="twelve">12-inch</label>
  </fieldset>

  <input type="text" name="total" readonly>
</form>

Check this on Fiddle
Hope this will be helpful.

查看更多
放荡不羁爱自由
4楼-- · 2020-03-26 18:36

You can use event delegation on the fieldset - whenever it observes a bubbled change event, you know that one of the <input>s has been selected, so you can then set the style of the total input field. Use { once: true } so that the listener only gets triggered once:

const total = document.querySelector('input[name="total"]');
document.querySelector('fieldset').addEventListener('change', () => {
  total.style.display = "inherit";
}, { once: true });
input[name="total"] {
  display: none;
}
<form>
<fieldset>
  <legend>Choose Size</legend>
  <label><input type="radio" name="size" id="six">6-inch</label>
  <label><input type="radio" name="size" id="eight">8-inch</label>
  <label><input type="radio" name="size" id="twelve">12-inch</label>
</fieldset>

<input type="text" name="total" readonly>
</form>

查看更多
登录 后发表回答