Looping gets - document.getElementById("random_0"
Looping gets - document.getElementById("random_0" + i).value = rnd;
经过大量修改的项目(感谢每个人的投入)我仍然不能循环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"
相关文章:
- 铬:“;未捕获的语法错误:意外的标记:"
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 使用“+="操作人员
- //而不是在src=“”上使用http://"属性
- "未捕获的语法错误:意外的标记}"
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- "实例范围”;TypeScript类的getter/setter
- Javascript复选框函数:;缺少:在属性id之后"
- "“;变量未引用正确的对象
- "日期“;AJAX请求返回的类型值未定义
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- "工具提示"jQuery插件坏了
- "锻造;React中的表达式
- 图像可以从源<img src=""/>.TEXT可以在没有javascript的情况下从外部
- 如何提取“;href"最近列表项中的属性值
- CKEditor如何允许href="javascript:void(0)"在小部件中
- 插入“;img src"在javascript中