【发布时间】:2023-03-10 07:54:02
【问题描述】:
这是我在这里的第一篇文章。 我的表格有问题。我希望能够仅获取表单上选中的框的值并将它们添加到输入之一。 问题是我要么得到所有复选框(选中或未选中),要么我得到未定义。 复选框是这样的
<input type="checkbox" className="checkbox" value="Triatholons" />
Triathlon Training Plans
</label>
```
//and the function I have been trying is this
function itworked() {
alert("Sky will contact you shortly!")
var values = document.getElementsByClassName("checkbox:checked")
console.log(values)
}
这将返回一个 HTML 集合,当我尝试提取值时,我只会得到未定义的值。 我在这里想念什么?谢谢您的帮助。
【问题讨论】:
-
将
getElementsByClassName替换为querySelectorAll,因为这将支持:checked后缀。你需要在类名之前使用.。 -
不使用[reactjs]标签为什么会有?
标签: javascript reactjs forms checkbox