在 HTML5 中多次单击输入按钮
我正在使用 HTML5 和 JavaScript 构建一个骰子游戏。
我希望玩家能够通过单击来选择骰子。这将阻止骰子被掷出。玩家应该能够再次单击骰子按钮,这将允许再次掷骰子。
我已经使用了代码,第一个 onclick 有效,但第二个 onclick 无效。
这是每个输入按钮只能单击一次的限制吗?还是我应该使用输入按钮以外的其他 HTML 标签?
我希望在不重新加载屏幕的情况下玩游戏。
HTML5
<tr>
<th><input type="button" id="diceOne" onclick="selectDice(this.id,this.value)" value="0"></input></th>
<th><input type="button" id="diceTwo" onclick="selectDice(this.id,this.value)" value="1"></input></th>
<th><input type="button" id="diceThree" onclick="selectDice(this.id,this.value)" value="2"></input></th>
<th><input type="button" id="diceFour" onclick="selectDice(this.id,this.value)" value="3"></input></th>
<th><input type="button" id="diceFive" onclick="selectDice(this.id,this.value)" value="4"></input></th>
</tr>
JavaScript
function selectDice(diceName,diceValue){
if (diceArray[diceValue][1]=="y"){
alert(diceArray[diceValue][1]);
document.getElementById(diceName).value = "Die now selected";
diceArray[diceValue][1]="n";
alert(diceArray[diceValue][1]);
}
else {
alert(diceArray[diceValue][1]);
document.getElementById(diceName).value = "Die not selected";
diceArray[diceValue][1]="y";
alert(diceArray[diceValue][1]);
}
}
I'm building out a dice game using HTML5 and JavaScript.
I want a player to be able to select a dice with an onlclick. This will stop the dice from being rolled. The player should be able to onclick the dice button again which will allow the dice to be rolled again.
I have played with the code and the first onclick works but the second onclick does not.
Is this a limitation of one onclick per input button or should I use another HTML tag other than input button?
I want the game to be played without reloading the screen.
HTML5
<tr>
<th><input type="button" id="diceOne" onclick="selectDice(this.id,this.value)" value="0"></input></th>
<th><input type="button" id="diceTwo" onclick="selectDice(this.id,this.value)" value="1"></input></th>
<th><input type="button" id="diceThree" onclick="selectDice(this.id,this.value)" value="2"></input></th>
<th><input type="button" id="diceFour" onclick="selectDice(this.id,this.value)" value="3"></input></th>
<th><input type="button" id="diceFive" onclick="selectDice(this.id,this.value)" value="4"></input></th>
</tr>
Javascript
function selectDice(diceName,diceValue){
if (diceArray[diceValue][1]=="y"){
alert(diceArray[diceValue][1]);
document.getElementById(diceName).value = "Die now selected";
diceArray[diceValue][1]="n";
alert(diceArray[diceValue][1]);
}
else {
alert(diceArray[diceValue][1]);
document.getElementById(diceName).value = "Die not selected";
diceArray[diceValue][1]="y";
alert(diceArray[diceValue][1]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您的代码中可能存在错误。我认为这是正在发生的事情:
我建议在没有“document.getElementById(diceName).value = ...”行的情况下尝试此代码,然后看看它是否有效。如果您确实需要更改 onclick 中的输入元素的值,那么我建议您不要使用内联 onclick,或者传入 diceName 和 diceValue 所需的文字值,如下所示:
It looks like you might have a bug in your code. I think this is what is going on:
I'd suggest trying this code without the "document.getElementById(diceName).value = ..." lines and see if it works then. If you really need to change the input element's value in your onclick, then I'd suggest either not using an inline onclick, or pass in the literal value you want for diceName and diceValue like so:
为每个按钮创建变量,并为 JavaScript 中的每次点击触发它们 true/false 作为条件,这应该可以修复您的第二次点击;)
create variables for each one of those buttons and trigger them true/false for each click in your javascript as a condition and that should fix your second click ;)