如何将结果添加到测验中? [英] How to add results to a quiz?
本文介绍了如何将结果添加到测验中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个测验代码,测验的目的是帮助人们为他们找到合适的手机。我已经尝试使用if语句将结果添加到此测验但是为了工作,我必须为每个单独的单选按钮分配一个if语句。这是我到目前为止的代码:
I have this code for a quiz and the aim of the quiz is to help people find the right phone for them. I have tried using the if statement to add results to this quiz but for that to work I would have to assign an if statement for every individual radio button. Here is my code so far:
<script type="text/javascript">
function validate() {
check1 = false;
check2 = false;
check3 = false;
check4 = false;
check5 = false;
check6 = false;
check7 = false;
check8 = false;
check9 = false;
check10 = false;
for (i = 0; i < 4; i++) {
if (testform.q1[i].checked) {
check1 = true;
}
}
for (i = 0; i < 6; i++) {
if (testform.q2[i].checked) {
check2 = true;
}
}
for (i = 0; i < 5; i++) {
if (testform.q3[i].checked) {
check3 = true;
}
}
for (i = 0; i < 4; i++) {
if (testform.q4[i].checked) {
check4 = true;
}
}
for (i = 0; i < 5; i++) {
if (testform.q5[i].checked) {
check5 = true;
}
}
for (i = 0; i < 4; i++) {
if (testform.q6[i].checked) {
check6 = true;
}
}
for (i = 0; i < 6; i++) {
if (testform.q7[i].checked) {
check7 = true;
}
}
for (i = 0; i < 5; i++) {
if (testform.q8[i].checked) {
check8 = true;
}
}
for (i = 0; i & lt; 5; i++) {
if (testform.q9[i].checked) {
check9 = true;
}
}
for (i = 0; i < 5; i++) {
if (testform.q10[i].checked) {
check10 = true;
}
}
if (check1 == false) {
alert("You missed Question 1.");
event.returnValue = false;
} else if (check2 == false) {
alert("You missed Question 2.");
event.returnValue = false;
} else if (check3 == false) {
alert("You missed Question 3.");
event.returnValue = false;
} else if (check4 == false) {
alert("You missed Question 4.");
event.returnValue = false;
} else if (check5 == false) {
alert("You missed Question 5.");
event.returnValue = false;
} else if (check6 == false) {
alert("You missed Question 6.");
event.returnValue = false;
} else if (check7 == false) {
alert("You missed Question 7.");
event.returnValue = false;
} else if (check8 == false) {
alert("Oops! You missed Question 8.");
event.returnValue = false;
} else if (check9 == false) {
alert("You missed Question 9.");
event.returnValue = false;
} else if (check10 == false) {
alert(You missed Question 10.");
event.returnValue=false;
}
}
// ]]></script>
<form action="result.php" method="post" name="testform" onsubmit="getResults">
<hr />
<h3>1. How much are you willing to spend on a phone per month?</h3>
<input type="radio" name="q1" value="A" /> A. £10-£15.
<input type="radio" name="q1" value="B" /> B. £15-£20.
<input type="radio" name="q1" value="C" /> C. £20-£25.
<input type="radio" name="q1" value="D" /> D. £25-£30.
<input type="radio" name="q1" value="E" /> E. £30-£35.
<input type="radio" name="q1" value="F" /> F. £35-£40.
<hr />
<h3>2. Are you good with technology?</h3>
<input type="radio" name="q2" value="A" /> A. Yes.
<input type="radio" name="q2" value="B" /> B. No.
<hr />
<h3>3. Are you looking for a simple phone?</h3>
<input type="radio" name="q3" value="A" /> A. Yes.
<input type="radio" name="q3" value="B" /> B. No.
<hr />
<h3>4. Are you looking for a modern type of phone?</h3>
<input type="radio" name="q4" value="A" /> A. Yes.
<input type="radio" name="q4" value="B" /> B. No.
<hr />
<h3>5. How big do you want the phone to be?</h3>
<input type="radio" name="q5" value="A" /> A. Big.
<input type="radio" name="q5" value="B" /> B. Medium.
<input type="radio" name="q5" value="C" /> B. Small.
<input type="radio" name="q5" value="D" /> B. I don't really mind.
<hr />
<h3>6. Do you care about the colour of the phone?</h3>
<input type="radio" name="q6" value="A" /> A. Yes.
<input type="radio" name="q6" value="B" /> B. No.
<hr />
<h3>7. Have you ever owned a mobile phone before?</h3>
<input type="radio" name="q7" value="A" /> A. Yes.
<input type="radio" name="q7" value="B" /> B. No.
<hr />
<h3>8. Do you want to be able to use the phone to get out of awkward social situations?</h3>
<input type="radio" name="q8" value="A" /> A. Yes.
<input type="radio" name="q8" value="B" /> B. No.
<hr />
<h3>9. Do you want to be able to access the app store and download apps using your phone?</h3>
<input type="radio" name="q9" value="A" /> A. Yes.
<input type="radio" name="q9" value="B" /> B. No.
<hr />
<h3>10. What happened to the last phone you owned?</h3>
<input type="radio" name="q10" value="A" /> A. I got bored of it.
<input type="radio" name="q10" value="B" /> B. It broke.
<input type="radio" name="q10" value="C" /> C. The contract ran out.
<input type="radio" name="q10" value="D" /> D. Other.
<input type="hidden" name="page" value="1" />
<center><input class="button" onclick="getResults()" type="submit" value="Submit" /> <input type="reset" value="Clear" /></center></form>
<textarea id="result">The right phone for you will be displayed here.</textarea>
如果您知道如何在文本区域显示不同的结果,具体取决于人们的单选按钮选项而不使用我已经使用过的方法请告诉我如何。
Please if you have any idea how I can display different results in the text area depending on people's radio button choices without using the method I have already used please tell me how.
推荐答案
首先要减少那个大代码使用jquery,我的解决方案是使用jquery,
首先在你的页面中首先包含jquery.js,然后在脚本块中写下以后验证:
First of all to reduce that large code use jquery, my solution is with jquery,
first include jquery.js in your page first then in script block write following to validate :
function validate() {
if (
( input [name = q1]:选中)。val()== undefined)
return msg( 1 );
if (
("input[name=q1]:checked").val() == undefined) return msg(1); if (
( input [name = q2]:checked)。val()== undefined)
return msg( 2 );
if (
("input[name=q2]:checked").val() == undefined) return msg(2); if (
这篇关于如何将结果添加到测验中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文