2017-07-02 96 views
-1

此示例適用「檢查」,但給出「遺漏的類型錯誤:無法讀取屬性未定義的‘檢查’」&&沒有工作,遺漏的類型錯誤:無法讀取屬性未定義

但不與& &物業工作兩if語句

如果(X ==真)& &如果(j == 「正確」)

\t function myFunction(){ 
 

 
\t \t var question = document.getElementById("question-1"); 
 
\t \t var options = question.getElementsByTagName("input"); 
 
\t \t 
 
\t \t for(var i=0;i<=options.length;i++){ 
 
\t \t \t 
 
\t \t \t var x = options[i].checked; 
 
\t \t \t var j = options[i].className; 
 
     // if(x == true) && if(j=="correct"){ - this does not work 
 
\t \t \t if(x == true){ 
 
\t \t \t \t if(j=="correct"){ 
 
\t \t \t \t \t question.getElementsByClassName("result")[0].innerHTML = "Your answer is correct"; \t 
 
\t \t \t \t }else{ 
 
\t \t \t \t \t question.getElementsByClassName("result")[0].innerHTML = "Your answer is wrong"; \t 
 
\t \t \t \t } 
 
\t \t \t } \t \t 
 
\t \t } 
 
\t };
\t <ul> 
 
\t \t <div id="question-1"> 
 
\t \t \t <li>What is a Computer Bug?</li> 
 
\t \t \t <input type="radio" name="selection" class="correct" onclick="myFunction()"><label>An error</label> 
 
\t \t \t <input type="radio" name="selection" class="wrong" onclick="myFunction()"><label>A Moth</label> \t 
 
\t \t \t <p class="result"></p> 
 
\t \t </div> 
 
\t </ul>

+1

這不是如何與IFS &&工作。應該是if(x == true && j ==「correct」) –

+0

謝謝。現在它可以工作 –

回答

2

使用i< options.length,而不是i<=options.length因爲options.length爲2,如果你使用=<那麼它會遍歷高達2<=2所以你2是不確定的。

function myFunction(){ 
 

 
\t \t var question = document.getElementById("question-1"); 
 
\t \t var options = question.getElementsByTagName("input"); 
 
\t 
 
\t \t for(var i=0;i<options.length;i++){ 
 
\t \t \t 
 
\t \t \t var x = options[i].checked; 
 
\t \t \t var j = options[i].className; 
 
     // if(x == true) && if(j=="correct"){ - this does not work 
 
\t \t \t if(x == true){ 
 
\t \t \t \t if(j=="correct"){ 
 
\t \t \t \t \t question.getElementsByClassName("result")[0].innerHTML = "Your answer is correct"; \t 
 
\t \t \t \t }else{ 
 
\t \t \t \t \t question.getElementsByClassName("result")[0].innerHTML = "Your answer is wrong"; \t 
 
\t \t \t \t } 
 
\t \t \t } \t \t 
 
\t \t } 
 
\t };
<ul> 
 
\t \t <div id="question-1"> 
 
\t \t \t <li>What is a Computer Bug?</li> 
 
\t \t \t <input type="radio" name="selection" class="correct" onclick="myFunction()"><label>An error</label> 
 
\t \t \t <input type="radio" name="selection" class="wrong" onclick="myFunction()"><label>A Moth</label> \t 
 
\t \t \t <p class="result"></p> 
 
\t \t </div> 
 
\t </ul>

+0

這個工程。謝謝 –

+0

很高興幫助你。 –

相關問題