2012-12-29 15 views
0

我仍然在學習JavaScript,所以任何使用完整建議總是受歡迎的。 我正在製作一個彩票模擬器,它的所有工作都很好,但..... 當我點擊函數運行時,隨機數被生成並進行比較,但是當我嘗試循環函數時,我得到了 - TypeError:document .getElementById(...)爲空。爲什麼不通過這個循環?正在獲取 - TypeError:document.getElementById(...)爲空

我已經嘗試了一些東西,但認爲我完全用這個撓我的腦袋。

有問題的代碼是。

var automateLottery = function(){ 
    numberOfWeeks = numberOfWeeks + 1;//tally up rounds 
    //drawUniqueNumbers(); 

var p_01 = parseInt(document.getElementById("picked_number_01").value); 
var p_02 = parseInt(document.getElementById("picked_number_02").value); 
var p_03 = parseInt(document.getElementById("picked_number_03").value); 
var p_04 = parseInt(document.getElementById("picked_number_04").value); 
var p_05 = parseInt(document.getElementById("picked_number_05").value); 
var p_06 = parseInt(document.getElementById("picked_number_06").value); 

var r_01 = parseInt(document.getElementById("random_01").value); 
var r_02 = parseInt(document.getElementById("random_02").value); 
var r_03 = parseInt(document.getElementById("random_03").value); 
var r_04 = parseInt(document.getElementById("random_04").value); 
var r_05 = parseInt(document.getElementById("random_05").value); 
var r_06 = parseInt(document.getElementById("random_06").value); 


if(p_01===r_01||p_01===r_02||p_01===r_03||p_01===r_04||p_01===r_05||p_01=== r_06){ 
     document.getElementById("compare_01").value = ("You have matched number " + " " +p_01); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_01").value = ("No matches") 
    }; 

if(p_02===r_01||p_02===r_02||p_02===r_03||p_02===r_04||p_02===r_05||p_02===r_06){ 
     document.getElementById("compare_02").value = ("You have matched number" + " " +p_02); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_02").value = ("No matches") 
    };  

if(p_03===r_01||p_03===r_02||p_03===r_03||p_03===r_04||p_03===r_05||p_03===r_06){ 
     document.getElementById("compare_03").value = ("You have matched number" + " " +p_03); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_03").value = ("No matches") 
    }; 

if(p_04===r_01||p_04===r_02||p_04===r_03||p_04===r_04||p_04===r_05||p_04===r_06){ 
     document.getElementById("compare_04").value = ("You have matched number" + " " +p_04); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_04").value = ("No matches") 
    }; 

if(p_05===r_01||p_05===r_02||p_05===r_03||p_05===r_04||p_05===r_05||p_05===r_06){ 
     document.getElementById("compare_05").value = ("You have matched number" + " " +p_05); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_05").value = ("No matches") 
    }; 

if(p_06===r_01||p_06===r_02||p_06===r_03||p_06===r_04||p_06===r_05||p_06===r_06){ 
     document.getElementById("compare_06").value = ("You have matched number" + " " +p_06); 
     addUpMatches(1); 
    }else{ 
     document.getElementById("compare_06").value = ("No matches") 
    }; 



declareMatches();//----Add up per round and total 
    document.getElementById("amountOfMatched").value = ("You have matched" + " " +totalMatchedThisRound + " "+ "numbers"); 
    document.getElementById("weeks").value = ("You have played for" + " " +numberOfWeeks + " "+ "and have won £"+ totalWinnings); 
    document.getElementById("amountOfThreeMatches").value = ("You have so far had" + " " +match3 + " "+ "matches of three" ); 
    document.getElementById("amountOffourMatches").value = ("You have so far had" + " " +match4 + " "+ "matches of four" ); 
    document.getElementById("amountOffiveMatches").value = ("You have so far had" + " " +match5 + " "+ "matches of five" ); 
    document.getElementById("amountOfsixMatches").value = ("You have so far had" + " " +match6 + " "+ "matches of six" ); 

    clearCount();//clears amount matched every round 
    cleartempTotal();//clears total matched every round 

}; 

for(var i = 0; i < 10 ; i++){ 
    automateLottery(); 
}; 

HTML:

<div id="random_numbers"> 
<FORM NAME="random_numbs" ACTION="" METHOD="GET"> 

    <INPUT TYPE="text" class="ran_numb" id="random_01" NAME="slot_01" VALUE=""> 
    <INPUT TYPE="text" class="ran_numb" id="random_02" NAME="slot_02" VALUE=""> 
    <INPUT TYPE="text" class="ran_numb" id="random_03" NAME="slot_03" VALUE=""> 
    <INPUT TYPE="text" class="ran_numb" id="random_04" NAME="slot_04" VALUE=""> 
    <INPUT TYPE="text" class="ran_numb" id="random_05" NAME="slot_05" VALUE=""> 
    <INPUT TYPE="text" class="ran_numb" id="random_06" NAME="slot_06" VALUE=""> 
    <br/><br/> 
    <INPUT TYPE="button" NAME="button2" Value="Draw Numbers"  onClick="drawUniqueNumbers()"> 
</FORM> 

<div id="matched_numbers"> 
<FORM NAME="myform" ACTION="" METHOD="GET"> 

    <INPUT TYPE="text" NAME="matched_01" id="compare_01" VALUE=""><br> 
    <INPUT TYPE="text" NAME="matched_02" id="compare_02" VALUE=""><br> 
    <INPUT TYPE="text" NAME="matched_03" id="compare_03" VALUE=""><br> 
    <INPUT TYPE="text" NAME="matched_04" id="compare_04" VALUE=""><br> 
    <INPUT TYPE="text" NAME="matched_05" id="compare_05" VALUE=""><br> 
    <INPUT TYPE="text" NAME="matched_06" id="compare_06" VALUE=""><br> 
    <br/> 
    <INPUT TYPE="text" NAME="numbers3" id="amountOfMatched" VALUE="You have matched 0 numbers"><br> 
    <br> 

    <INPUT TYPE="text" NAME="numbers4" id="amountOfThreeMatches" VALUE=""><br> 
    <INPUT TYPE="text" NAME="numbers5" id="amountOffourMatches" VALUE=""><br> 
    <INPUT TYPE="text" NAME="numbers6" id="amountOffiveMatches" VALUE=""><br> 
    <INPUT TYPE="text" NAME="numbers6" id="amountOfsixMatches" VALUE=""><br> 
    Stats<br> 
    <INPUT TYPE="text" NAME="weeks" id="weeks" VALUE=""><br> 
</FORM> 

這是一些HTML的。

只是爲了確認有值進入形式和變量。 我設置了一個按鈕來觸發功能正常的「automateLottery」功能。 它會生成隨機數字,並將其與所選數字進行比較,然後編譯統計數據。 問題是當我在循環中設置automateLottery時,它不起作用。

我應該把所有的代碼放在這裏嗎?或者這足夠嗎?

再次感謝

在此先感謝任何人誰可以提供幫助。

+3

是否存在具有該「id」的元素? – Blender

+4

請添加與此一起使用的HTML。獲取空數據的原因是因爲您試圖引用不包含任何內容的HTML節點。 – L0j1k

+0

只需提一下:你永遠不應該寫這樣重複的代碼。總有一個更好的解決方案。在這個例子中,你應該在數組中保存'p'值,然後檢查你的p數組是否包含每個'r'值。 – AmShaegar

回答

0

首先,您的null異常可能來自空元素或不存在的html元素。

其次,使用循環來處理所有匹配的語句。

例如:

for (var i = 1; i = <6; i++) 
{ 
    var variable = parseInt(document.getElementById("picked_number_" + i).value); 
    if (variable == Math.floor((Math.random()*100)+1)) 
    { 
     alert("You've won!"); 
    } 
} 

在這種情況下,你只必須有id爲「picked_number_」,並在循環使用的可變數量的一些正確的HTML元素。 (VAR我)你隨意在if語句生成的,如果這是符合你贏了:d

順便說一句,這個腳本,使用1到100之間

希望這有助於你的隨機數小。

+0

謝謝科西安。它比這更復雜一點。 Plaese在http://jsfiddle.net/Ulleruller/2q2ne/7/看看它的工作。我只是不能循環它! – UllerUller

+0

但我會用循環來匹配語句謝謝。 – UllerUller