0

我仍在学习 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 时,它不起作用。

我应该把我所有的代码放在这里吗?或者这已经足够了吗?

再次感谢

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

4

1 回答 1

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 之间的随机数。

希望这对您有所帮助。

于 2012-12-29T12:03:13.527 回答