如何使用 JavaScript 选择每个问题的答案?例如获取所有 q1、q2 等。
我已经尝试过:
document.getElementById(formResults)[input = "q1"]
但似乎不起作用。
代码如下:
<form id="formResult">
<h1>Q1) According to the old proverb all roads lead to which capital city</h1>
<input type="radio" name="q1" value="a" id="q1a"> London <br />
<input type="radio" name="q1" value="b" id="q1b"> Rome <br />
<input type="radio" name="q1" value="c" id="q1c"> New York <br />
<h1>Q2) The name of which football club is an anagram of `Red Admiral`?</h1>
<input type="radio" name="q2" value="a" id="q2a"> Red Devils <br />
<input type="radio" name="q2" value="b" id="q2b"> Real Madrid <br />
<input type="radio" name="q2" value="c" id="q2c"> Roma <br />
<h1>Q3) In what year was Google launched on the web?</h1>
<input type="radio" name="q3" value="a" id="q3a"> 1998 <br />
<input type="radio" name="q3" value="b" id="q3b"> 1995 <br />
<input type="radio" name="q3" value="c" id="q3c"> 2001 <br />
<h1>Q4) In computing what is Ram short for?</h1>
<input type="radio" name="q4" value="a" id="q4a"> Random Access Memory <br />
<input type="radio" name="q4" value="b" id="q4b"> Real Access Memory <br />
<input type="radio" name="q4" value="c" id="q4c"> Rough Access Memory <br />
<h1>Q5) What does HTML stand for?</h1>
<input type="radio" name="q5" value="a" id="q5a"> Hyperlinks and Text Markup Language <br />
<input type="radio" name="q5" value="b" id="q5b"> Home Tool Markup Language <br />
<input type="radio" name="q5" value="c" id="q5c"> Hyper Text Markup Language <br />
<input type="submit" value= "Check answers">
</form>
提前致谢
最佳答案
您可以使用getElementsByName
查找具有特定名称的所有单选按钮:
document.getElementById('formResult').getElementsByName('q1')
您还可以使用document.querySelectorAll
document.querySelectorAll('#formResult [name=q1]');
关于javascript - 在 javascript 中选择输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36415311/