所以我遇到的问题是我的JS游戏中的复选框没有响应单个点击。当我在console.log时,每次都得到索引0,无论我点击哪个框和非响应复选框。但是,关卡功能正在响应。
window.onload = function() {
console.log('DOM is loaded');
fillAll = function() {
// event.preventDefault();
for (var i = 0; i < 25; i++) {
document.bulbform.elements[i].checked = 1;
}
};
clearAll = function() {
for (var i = 0; i < 25; i++) {
document.bulbform.elements[i].checked = 0;
}
};
levelOne = function() {
clearAll();
document.bulbform.elements[10].checked = 1;
document.bulbform.elements[12].checked = 1;
document.bulbform.elements[14].checked = 1;
};
levelTwo = function() {
clearAll();
document.bulbform.elements[12].checked = 1;
document.bulbform.elements[16].checked = 1;
document.bulbform.elements[17].checked = 1;
document.bulbform.elements[18].checked = 1;
document.bulbform.elements[20].checked = 1;
document.bulbform.elements[21].checked = 1;
document.bulbform.elements[22].checked = 1;
document.bulbform.elements[23].checked = 1;
document.bulbform.elements[24].checked = 1;
};
levelThree = function() {
fillAll();
document.bulbform.elements[4].checked = 0;
document.bulbform.elements[6].checked = 0;
document.bulbform.elements[7].checked = 0;
document.bulbform.elements[8].checked = 0;
document.bulbform.elements[11].checked = 0;
document.bulbform.elements[12].checked = 0;
document.bulbform.elements[13].checked = 0;
document.bulbform.elements[16].checked = 0;
document.bulbform.elements[17].checked = 0;
document.bulbform.elements[18].checked = 0;
document.bulbform.elements[24].checked = 0;
};
levelFour = function() {
clearAll();
document.bulbform.elements[2].checked = 1;
document.bulbform.elements[6].checked = 1;
document.bulbform.elements[8].checked = 1;
document.bulbform.elements[10].checked = 1;
document.bulbform.elements[12].checked = 1;
document.bulbform.elements[14].checked = 1;
document.bulbform.elements[16].checked = 1;
document.bulbform.elements[18].checked = 1;
document.bulbform.elements[22].checked = 1;
};
levelFive = function() {
clearAll();
document.bulbform.elements[11].checked = 1;
document.bulbform.elements[16].checked = 1;
document.bulbform.elements[21].checked = 1;
};
checkAll = function() {
var win = 1;
for (var i = 0; i < 25; i++) {
if (document.bulbform.elements[i].checked == 1) {
win = 0;
}
}
if (win == 1) {
alert("You Won!");
}
};
check = function(v) {
a = parseInt(v);
// console.log(parseInt(a));
// console.log(typeof(a));
if (document.bulbform.elements[v].checked == 1) {
document.bulbform.elements[v].checked = 0;
} else {
document.bulbform.elements[v].checked = 1;
}
checkAll();
};
};
<body>
<div class="row">
<div class="col s12 m6">
<div class="card-panel blue-grey lighten-3">
<form name="bulbform">
<!-- Row One -->
<input type="checkbox" value="0" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="1" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="2" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="3" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="4" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<!-- Row Two -->
<input type="checkbox" value="5" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="6" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="7" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="8" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="9" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<!-- Row Three -->
<input type="checkbox" value="10" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="11" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="12" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="13" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="14" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<!-- Row Four -->
<input type="checkbox" value="15" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="16" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="17" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="18" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="19" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<!-- Row Five -->
<input type="checkbox" value="20" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="21" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="22" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="23" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<input type="checkbox" value="24" onclick="javascript:check(value)" class="filled-in" id="filled-in-box" checked="1" />
<label for="filled-in-box"></label>
<br>
<!-- Stat Game Levels -->
<a href="javascript:levelOne()" class="waves-effect waves-teal btn-flat">Level 1</a>
<a href="javascript:levelTwo()" class="waves-effect waves-teal btn-flat">Level 2</a>
<a href="javascript:levelThree()" class="waves-effect waves-teal btn-flat">Level 3</a>
<a href="javascript:levelFour()" class="waves-effect waves-teal btn-flat">Level 4</a>
<a href="javascript:levelFive()" class="waves-effect waves-teal btn-flat">Level 5</a>
<!-- End Game Levels -->
<br>
<button class="btn waves-effect waves-light btn-large" type="reset" value="clear" name="action">Clear
<i class="material-icons left">lightbulb_outline</i>
</button>
</div>
</div>
</div>
<script type="text/javascript" src="game.js"></script>
</body>
答案 0 :(得分:2)
... onclick="javascript:check(value)" ...
没有全局值变量,因此您传递 undefined ,其值为0.您实际想要传递的是复选框的值:
... onclick="javascript:check(this.value) ...
哦,还有:
我的JS游戏中的复选框没有响应个别点击
因为你在做:
if (document.bulbform.elements[v].checked == 1) {
document.bulbform.elements[v].checked = 0;
} else {
document.bulbform.elements[v].checked = 1;
}
因此,如果未选中复选框然后单击它,则会检查侦听器中的状态,因此您再次取消选中它。而反之亦然如果选中它(用户点击,取消选中它,那么监听器再次检查它)。这是荒谬的,只需删除监听器,让复选框本身就是一个复选框。
答案 1 :(得分:0)
我猜错了。 1)默认情况下,您单击复选框为true 2)单击复选框后,它将变为false,但您的代码已
if (document.bulbform.elements[v].checked == 1) {
document.bulbform.elements[v].checked = 0;
} else {
document.bulbform.elements[v].checked = 1;
}
将复选框再次设置为true。因此,每当您尝试单击该复选框时,选中将在其状态更改时返回false,并且您的代码更改将再次更改为true
从代码中删除上述代码。它会正常工作。