javascript - 在 javascript 中选择输入

标签 javascript

如何使用 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/

相关文章:

javascript - 使用带连字符的键解构对象

javascript - 无法返回动态创建的复选框的值

javascript - 从下拉列表中选择选项时响应警告

javascript - 如何使用 UI Bootstrap?

javascript - 雅格斯 : access processed argv in fail function

javascript - Moment js 检查某个起始日期和截止日期是否在本周

javascript - 构建流畅的马赛克网格

javascript - 在 EaselJS 中将矢量绘制到位图

javascript - 使用 ng-init 时复选框选择丢失

javascript - 使用 <use> 动态创建时,SVG <animate> 不起作用