Looping gets - document.getElementById("random_0"

Looping gets - document.getElementById("random_0" + i).value = rnd;

本文关键字:quot random gets document getElementById Looping      更新时间:2023-09-26

经过大量修改的项目(感谢每个人的投入)我仍然不能循环automateLottery();我的控制台一直告诉我-

TypeError: document.getElementById(…)为空文档。getElementById("random_0" + i).value = rnd;

我已经把我的项目放在http://jsfiddle.net/Ulleruller/2q2ne/17/

谁能告诉我这是什么意思?
T 'anks

 var match3 = 0, match4 = 0, match5 = 0, match6 = 0;
 var startLottery = false;
 var numberOfWeeks = 0;
 var numberOfMatches = 0;
 var totalWinnings = 0;
 var totalMatchedThisRound=0;
 var matchedNumbers = 0;
 var addUpMatches = function(matched){
matchedNumbers = matchedNumbers + matched;//---add up amount of matched numbers
totalMatchedThisRound = matchedNumbers; // ----store in new variable for 
};
var declareMatches = function(){//----declare matched numbers and keep totals 
switch(totalMatchedThisRound){
        case 3:
        console.log("Ching!! 3 matches");
        //console.log(amountOfThreeMatches);
        match3 = match3 + 1;
        totalWinnings = totalWinnings + 10;
        break;
        case 4:
        console.log("Ching!! 4 matches");
        match4 = match4 + 1;
        totalWinnings = totalWinnings + 70;
        break;
        case 5:
        console.log("Ching!! 5 matches");
        match5 = match5 + 1;
        totalWinnings = totalWinnings + 60000;
        break;
        case 6:
        console.log("Ching!! 6 matches");
        match6 = match6 + 1;
        totalWinnings = totalWinnings + 1000000;
        break;
        default:
        console.log("Alas only 2 or less matches");
        break;
           };
                   };
var clearCount = function(matched){ 
matchedNumbers = 0; //clears amount of matched numbers per round
};
var cleartempTotal = function(matched){ 
totalMatchedThisRound = 0; //clears amount of matched numbers per round
};
var start = function(){
startLottery = true;
};
var stop = function(){
startLottery = false;
};

function chooseNumbers() {//---chosen numbers check for doubles
    var checkForDouble ;
    var pickedNumbersArray= [];
    for(var t = 1; t < 7; t++){
        checkForDouble = parseInt(document.getElementById("picked_number_0" + t ).value);
                        document.getElementById("confirm_0" + t).value =  ("You have picked"+" " +document.getElementById("picked_number_0" + t ).value);
                        document.getElementById("confirm_0" + t).style.backgroundColor="green";
        for(var s = 0; s < 6; s++){
                if(checkForDouble == pickedNumbersArray[s]){
                    document.getElementById("confirm_0" + t).value =  ("Already chosen");
                    document.getElementById("confirm_0" + t).style.backgroundColor="red"
                }
        }
    pickedNumbersArray.push(checkForDouble);
    }
    };

var drawLotteryNumbers = function() { //generate numbers ensure unique
 var compareArray= []; 
 var rnd =0;
for(var i = 1; i < 7; i++){
            rnd = Math.floor((Math.random()*10)+1);
    while(rnd === compareArray[0]||rnd === compareArray[1]||rnd === compareArray[2]||rnd    === compareArray[3]||rnd === compareArray[4]||rnd === compareArray[5]){
                rnd = Math.floor((Math.random()*10)+1);
            };
            compareArray.push(rnd);
            document.getElementById("random_0" + i).value = rnd;
                    };
                };              
function automateLottery(){// generate - compare - stats
numberOfWeeks = numberOfWeeks + 1;//tally up rounds
drawLotteryNumbers();
var pickedNumber;
    for(var e = 1; e < 7; e++){
        document.getElementById("compare_0" + e).style.backgroundColor="white";
        document.getElementById("compare_0" + e).value=("No matches");
        pickedNumber = parseInt(document.getElementById("picked_number_0" + e).value);
        for(var i = 1; i < 7; i++){
            if(pickedNumber == document.getElementById("random_0" + i).value){
                document.getElementById("compare_0" + e).style.backgroundColor="green";
                document.getElementById("compare_0" + e).value=("You have matched number" + " "+ pickedNumber );
                addUpMatches(1); 
            }
        }
    }
    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 + " "+ "weeks" );
    document.getElementById("moneyWon").value = ("You have won in total £"+ 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
  };

 while(startLottery === true){
automateLottery();
  };

这就是它告诉你的。没有id为"random_0" + i的元素在本例中是"random_01"到"random_06"