【发布时间】:2019-06-07 16:51:48
【问题描述】:
我有一个表单/问卷,用户必须在其中选择 HTML 中的各种选项。 然后 Javascript 将添加所有选项;有更多的表格将被加起来以创建一个总计。 我知道我需要使用 parseInt 和各种 if 语句: 如果选择选项 1,则返回值 如果选择了选项 2,则返回值 等等..
HTML:
<input type="radio" name="age" id= "age1" value="0" checked> 1-25<br>
<input type="radio" name="age" id= "age2" value="5"> 26-40<br>
<input type="radio" name="age" id= "age3" value="8"> 41-60<br>
<input type="radio" name="age" id= "age4" value="10"> 60+<br>
Javascript:
calculateAge () {
var age = parseInt (document.getElementById('age1').value)
if (age = checked) return age;
console.log('age')
}
【问题讨论】:
-
你的问题是什么?你的代码有什么用?
-
您可以简单地让适当的 CSS 选择器完成查找选中元素的工作(请参阅我的答案)。这样您就不必检查每个单选按钮是否被选中。
标签: javascript html radio checked