-6

我正在创建一个用于模块评估的测验模板。在此过程中,我创建了一个带有单选按钮的 True/False 问题。当我单击得分按钮检查我的答案时,我得到属性“if”的值为 null 或未定义,而不是函数对象。”我检查了单选按钮 id 以确保它与我的 If ( document.getElementById('answer_true').checked) 声明。我所有其他带有单选按钮的多项选择题都可以正常工作。我似乎无法将手指放在问题的根源上。得分按钮调用 check_tfQ 函数并告诉我问题出在 if 语句中。任何帮助将不胜感激。

<script language="JavaScript" type="text/javascript">
<!--
// variable declarations
score=0;
counter=0;
c=1;
correct="Correct! ";
incorrect="Incorrect! ";
// function declarations for each question

function Question1()
{
document.getElementById("main_ques").innerHTML="1. Put question 1 here.";
document.getElementById("choice1").innerHTML="Put answer 1.1 here.";
document.getElementById("choice2").innerHTML="Put answer 1.2 here.";
document.getElementById("choice3").innerHTML="Put answer 1.3 here.";
document.getElementById("counter").innerHTML="Counter is "+c;
}
function checkQ1()
{
c+=1;
if (document.getElementById('answer_a').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById('answer_a').checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
function Question2()
{
document.getElementById("main_ques").innerHTML="2. Put question two here.";
document.getElementById("choice1").innerHTML="Put answer 2.1 here.";
document.getElementById("choice2").innerHTML="Put answer 2.2 here.";
document.getElementById("choice3").innerHTML="Put answer 2.3 here.";
document.getElementById("counter").innerHTML="Counter is "+c;
}
function checkQ2()
{
c+=1;
if (document.getElementById('answer_a').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById('answer_a').checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
function Question3()
{
document.getElementById("main_ques").innerHTML="3. Put question 3 here.";
document.getElementById("choice1").innerHTML="Put answer 3.1 here.";
document.getElementById("choice2").innerHTML="Put answer 3.2 here.";
document.getElementById("choice3").innerHTML="Put answer 3.3 here.";
document.getElementById("counter").innerHTML="Counter is "+c;
}
function checkQ3()
{
c+=1;
if (document.getElementById('answer_b').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById('answer_b').checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
function Question4()
{
document.getElementById("main_ques").innerHTML="4. Put question 4 here.";
document.getElementById("choice1").innerHTML="Put answer 4.1 here.";
document.getElementById("choice2").innerHTML="Put answer 4.2 here.";
document.getElementById("choice3").innerHTML="Put answer 4.3 here.";
document.getElementById("counter").innerHTML="Counter is "+c;
}
function checkQ4()
{
c+=1;
if (document.getElementById('answer_a').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById('answer_a').checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
function Question5()
{
document.getElementById("main_ques").innerHTML="5. Put question 5 here.";
document.getElementById("choice1").innerHTML="Put answer 5.1 here.";
document.getElementById("choice2").innerHTML="Put answer 5.2 here.";
document.getElementById("choice3").innerHTML="Put answer 5.3 here.";
document.getElementById("counter").innerHTML="Counter is "+c;
}
function checkQ5()
{
c+=1;
if (document.getElementById('answer_a').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById('answer_a').checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
// function reveals fill in the blank question
function fitbQ()
{
document.getElementById("fitb_ques").style.visibility="visible";
document.getElementById("main_ques").innerHTML="";
document.getElementById("answer_a").style.visibility="hidden";
document.getElementById("answer_b").style.visibility="hidden";
document.getElementById("answer_c").style.visibility="hidden";
document.getElementById("choice1").innerHTML="";
document.getElementById("choice2").innerHTML="";
document.getElementById("choice3").innerHTML="";
document.getElementById("counter").innerHTML="Counter is "+c;
}
// function checks fill in the blank question
function check_fitbQ()
{
c+=1;
var fitb_answer=document.getElementById("fitb_ans").value;
if (fitb_answer=="balloon")
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
else if (document.getElementById("fitb_ans").checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
}
}
// function for True/False question
function tfQ()
{
document.getElementById("main_ques").innerHTML="";
document.getElementById("fitb_ques").style.visibility="hidden";
document.getElementById("answer_a").style.visibility="hidden";
document.getElementById("answer_b").style.visibility="hidden";
document.getElementById("answer_c").style.visibility="hidden";
document.getElementById("choice1").innerHTML="";
document.getElementById("choice2").innerHTML="";
document.getElementById("choice3").innerHTML="";
document.getElementById("counter").innerHTML="Counter is "+c;
document.getElementById("tf_ques").innerHTML="7. Put question 7 here.";
document.getElementById("tf_ques").style.visibility="visible";
document.getElementById("T").style.visibility="visible";
document.getElementById("F").style.visibility="visible";
document.getElementById("answer_true").style.visibility="visible";
document.getElementById("answer_false").style.visibility="visible";
document.getElementById("T").innerHTML="True";
document.getElementById("F").innerHTML="False";
}
// function checks True/False question
function check_tfQ()
{
c+=1;
If (document.getElementById('answer_true').checked)
{
score+=1;
document.getElementById("reply_b").innerHTML=correct;
document.getElementById("testscore").innerHTML=score;
}
/* else if (document.getElementById("answer_true").checked==false)
{
document.getElementById("reply_a").innerHTML=incorrect;
} */ 
}
// function declaration for clearing the form
function clearform(form)
{
document.Questions.reset(form);
document.getElementById("reply_a").innerHTML="";
document.getElementById("reply_b").innerHTML="";
document.getElementById("testscore").innerHTML="";
}
// function declaration to disable all radio buttons after checking answers
function holdAnswers()
{
var obj=document.forms["Questions"]; 
            for (i=0;i<obj.length;i++) { 
                with (obj[i]) { 
                    if (type=='radio') { 
                        disabled=true; 
                    } 
                } 
            } 
}
function releaseAnswers()
{
var obj=document.forms["Questions"]; 
            for (i=0;i<obj.length;i++) { 
                with (obj[i]) { 
                    if (type=='radio') { 
                        disabled=false; 
                    } 
                } 
            } 
}
// function changes questions after each answer
function qlogger()
{
switch (c)
{
case 1:
Question1();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 2:
Question2();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 3:
Question3();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 4:
Question4();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 5:
Question5();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 6:
fitbQ();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 7:
tfQ();
releaseAnswers();
document.getElementById("tally").disabled=false;
break;
case 8:
results(score,c);
break;
}
}
//function declaration to check all answers to questions and provide output
function checkAll(c)
{
switch (c)
{
case 1:
checkQ1();
break;
case 2:
checkQ2();
break;
case 3:
checkQ3();
break;
case 4:
checkQ4();
break;
case 5:
checkQ5();
break;
case 6:
check_fitbQ();
break;
case 7:
check_tfQ();
break;
case 8:
results();
}
holdAnswers();
document.getElementById("tally").disabled=true;
}
function results(score,c)
{
grade=Math.round((score/(c-1))*100);
document.getElementById("main_ques").innerHTML="";
document.getElementById("answer_a").style.visibility="hidden";
document.getElementById("answer_b").style.visibility="hidden";
document.getElementById("answer_c").style.visibility="hidden";
document.getElementById("choice1").innerHTML="";
document.getElementById("choice2").innerHTML="";
document.getElementById("choice3").innerHTML="";
document.getElementById("counter").innerHTML="";
document.getElementById("tally").style.visibility="hidden";
if (grade>=80)
{
document.getElementById("results1").innerHTML="Congratulations! <br/>"; 
document.getElementById("results2").innerHTML="You passed with a score of "+ grade+"%";
}
else
{
document.getElementById("results1").innerHTML="Sorry. <br>";
document.getElementById("results2").innerHTML="You failed with a score of "+grade+"%";
}
}
//-->
</script> 
</head>
<body onload="clearform(); qlogger()">
<div id="title">Module Title</div>
<div id="subtitle">Module Subtitle</div>
<div class="questions">
<form name="Questions">
<!-- Multiple choice question placeholder is here -->
<div id="main_ques">1. Please state your question here?</div>
<!-- Correct or Incorrect response -->
<div id="reply_a" class="response"></div>
<div id="reply_b" class="response2"></div>
<!-- Test score displays here -->
<div id="testscore" class="showscore"></div>
<!-- Test results displays here -->
<div id="results1" class="results1"></div>
<div id="results2" class="results2"></div>
<br>
<!-- Multiple choice radio buttons go here -->
<input type="radio" name="Q_ans" id="answer_a" class="answer_a" value="ans_a"/><br><div id="choice1" class="choice1"></div>
<input type="radio" name="Q_ans" id="answer_b" class="answer_b" value="ans_b"/><br><div id="choice2" class="choice2"></div>
<input type="radio" name="Q_ans" id="answer_c" class="answer_c" value="ans_c"/><br><div id="choice3" class="choice3"></div>
<!-- Put fill in the blank question here -->
<div id="fitb_ques" class="fitb_ques">6. Please state your fill in the <input name="fitb_ans" id="fitb_ans"> question here.</div>
<!-- Put True/False question here -->
<div id="tf_ques" class="tf_ques">7. Please state your T/F question here.</div>
<input type="radio" name="TF_ans" id="answer_true" class="true_btn" value="true_ans"/><br><div id="T" class="T">Put choice one here.</div>
<input type="radio" name="TF_ans" id="answer_false" class="false_btn" value="false_ans"/><br><div id="F" class="F">Put choice two here.</div> 
<div id="counter"></div>
<br>
<input type="button" name="tally" id="tally" value="score" onclick="checkAll(c)">
</form>
</div>
<div id="nextbutton">
<input type="image" src="pics/next_button.png" alt="Next Button" height="50" width="120" onclick="clearform();qlogger()">
</div>
</body>
</html>
4

1 回答 1

8

你有一个大写的“我”在If (...。将其更改为小写:if (.... JavaScript 区分大小写。

于 2012-12-20T19:08:05.200 回答