当您点击复选框时,如何使用JavaScript确保输入字段获取数字0?

huangapple go评论103阅读模式
英文:

How do I make sure with JavaScript that when I click on the checkbox, the input field gets the number 0?

问题

这是包含复选框和具有id="number"的输入字段的gohtml代码。

如何使用JavaScript确保当复选框被点击时,所有的输入字段都自动填入数字0?

问题是当我点击复选框时,0只会填入第一个字段,而不是其他字段。

提前感谢!

英文:

This is the gohtml code containing the checkbox and the input fields with id="number"

How do I ensure with JavaScript that when the checkbox is clicked, all the input fields are automatically entered with the number 0?

The problem is that the 0 is entered in the first field, but not in the other fields when I click on the checkbox.

Thanks in advance!

答案1

得分: 1

我需要更多的信息,但是为了回答你的问题,我假设你的复选框的id是"chkbox"。

我们可以给复选框添加一个事件监听器:

let checkbox = document.getElementById("chkbox");
let inp = document.getElementById("number");
checkbox.addEventListener('change', function() {

  if (this.checked) {
      inp.value = 0;
  }
});

这样应该可以解决问题。

英文:

I need more than that, but for answering I am assuming your checkbox has id=chkbox

what we can do is add an event listener to the checkbox

let checkbox = document.getElementById("chkbox");
let inp = document.getElementById("number");
checkbox.addEventListener('change', function() {

  if (this.checked) {
      inp.value = 0;
  }
});

This should do the trick

huangapple
  • 本文由 发表于 2022年3月18日 19:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/71526126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定