繁体   English   中英

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

[英]Why wont this loop? Getting - TypeError: document.getElementById(…) is null

我仍在学习JavaScript,因此总是欢迎您使用任何建议。 我正在制作一个彩票模拟器,它的所有功能都很好,但是.....当我单击要运行的函数时,将生成随机数并进行比较,但是当我尝试循环该函数时,我会得到-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时,它将无法工作。

我应该在这里放置所有代码吗? 还是足够?

再次感谢

在此先感谢任何可以提供帮助的人。

首先,您的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 i)您的随机数是在if语句中生成的,如果匹配则您已经赢得了:D

顺便说一句,此脚本使用的是1到100之间的随机数。

希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM