为什么不循环呢?获取 - TypeError:document.getElementById(...)为null

时间:2012-12-29 11:36:20

标签: javascript

我仍然在学习JavaScript,所以任何使用完整的建议总是受欢迎的。 我正在制作一个彩票模拟器,它一切运作良好,但..... 当我单击要运行的函数时,会生成随机数并进行比较,但是当我尝试循环我得到的函数时 - TypeError:document.getElementById(...)为null。

我尝试了一些事情,但我认为我完全不知所措。

有问题的代码是。

 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时,它将无法正常工作。

我应该把所有代码放在这里吗?或者这就够了吗?

再次感谢

提前感谢任何可以提供帮助的人。

1 个答案:

答案 0 :(得分: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!");
    }
}

在这种情况下,您只需要一些正确的html元素,其id为“picked_number_”,并且循环中使用的变量号。 (var i)你的随机数是在if语句中生成的,如果匹配你就赢了:D

顺便说一下,这个脚本使用1到100之间的随机数。

希望这对你有所帮助。