2012-12-20 71 views
-5

我正在創建一個測驗模板,我將用於模塊評估。在這個過程中,我用單選按鈕創建了一個True/False問題。當我點擊分數按鈕來檢查我的答案時,我得到的屬性值「if」爲空或未定義,而不是一個Function對象。「我檢查了單選按鈕ID以確保它與我的If( document.getElementById('answer_true')。checked)語句我用單選按鈕的所有其他選擇題都可以正常工作,我似乎無法指出問題的根源,分數按鈕調用check_tfQ函數並告訴我問題出在if語句任何幫助,將不勝感激屬性「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> 
+2

在哪一行?你真的希望我們閱讀所有的縮進代碼嗎? – Femaref

+0

格式化您的代碼將使其人們更容易幫助您十億倍...... – jahroy

+2

將來,請嘗試將您發佈的代碼限制爲僅適用於相關的代碼。通過幾十(或幾百)行無關代碼來查找重要部分是非常困難的。例如,在這種情況下,由於調用'check_tfQ()'時發生問題,所以您只能發佈該函數。 –

回答

8

你有If (...「I」大寫將其更改爲小寫:。if (... JavaScript是區分大小寫的

+0

+1爲你的努力 – jAndy

+0

這就是當一個IDE派上用場時,呵呵? ;) – DanMan

+1

@DanMan - Nah。 OP說這個錯誤是在調用'check_tfQ'的時候出現的,所以我查了一下函數的名字,就這樣了。 –

相關問題