这是我的代码。我想为mcqs做一个基于javascript的测验。在进行verify()函数时出现问题。如何选择用户选择的选项,然后将其与该问题的正确答案匹配并告知状态?感谢
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20141113</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-distribution</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>pellet-profiler-ignazio1977</artifactId>
<version>2.4.0-ignazio1977</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
function verify() {
var correct = 1;
var selected = 0;
if (getElementByID.r1) selected = 1;
if (selected == correct) window.alert("You are right");
else window.alert("You WRong!!!");
}
答案 0 :(得分:1)
使用单选按钮,您希望它们具有相同的name
。这样当你检查一个时,其他的将被取消选中。
此外,getElementByID.r1
没有做任何事情。 getElementById
(请注意大小写)是document
的属性,是函数。
document.getElementById('r1')
然后,使用单选按钮,您可以使用.checked
属性查看是否已选中。
if (document.getElementById('r1').checked) selected = 1;
这是一个更新的演示:
function verify() {
var correct = 1;
var selected = 0;
if (document.getElementById('r1').checked) selected = 1;
if (selected == correct) window.alert("You are right");
else window.alert("You WRong!!!");
}
<img src="//placekitten.com/g/50/50" alt="x" />
<br><br><br>
<input type="radio" name="quiz" id=r1 /><strong>1. </strong>
<p id=ans1>This is cloud</p>
<br />
<input type="radio" name="quiz" id=r2 /><strong>2. </strong>
<p id=ans2>This is hat</p>
<br />
<input type="radio" name="quiz" id=r3 /><strong>3. </strong>
<p id=ans3>This is rough</p>
<br />
<input type="radio" name="quiz" id=r4 /><strong>4. </strong>
<p id=ans4>This is none</p>
<br />
<input type="button" onclick=verify(); value="send" />
<input type="button" onclick=reveal(); value="Reveal Solution" />