简体   繁体   English

Javascript 问答测验不起作用

[英]Javascript Q&A Quiz not working

here is my code.这是我的代码。 I want to make a javascript based quiz for mcqs.我想为 mcqs 制作一个基于 javascript 的测验。 There is a problem while making the verify() function.制作 verify() 函数时出现问题。 How to select the user selected option and then match it with the correct answer of that question and tell the status?如何选择用户选择的选项,然后将其与该问题的正确答案匹配并告知状态? Thanks谢谢

 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!!!"); }
 <img src="//placekitten.com/g/50/50" alt="x" /> <br><br><br> <input type="radio" id=r1 /><strong>1. </strong> <p id=ans1>This is cloud</p> <br /> <input type="radio" id=r2 /><strong>2. </strong> <p id=ans2>This is hat</p> <br /> <input type="radio" id=r3 /><strong>3. </strong> <p id=ans3>This is rough</p> <br /> <input type="radio" 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" />

With radio buttons, you want them all to have the same name .对于单选按钮,您希望它们都具有相同的name That way when you check one, the others will be unchecked.这样,当您选中一个时,其他的将不被选中。

Also, getElementByID.r1 doesn't do anything.此外, getElementByID.r1不做任何事情。 getElementById (note the capitalization) is a property of document and is a function . getElementById (注意大写)是document一个属性,是一个function

document.getElementById('r1')

Then, with radio buttons, you use the .checked property to see if it was checked or not.然后,通过单选按钮,您可以使用.checked属性查看它是否被选中。

if (document.getElementById('r1').checked) selected = 1;

Here's an updated demo:这是一个更新的演示:

 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM