在 HTML5 中多次单击输入按钮

发布于 2024-11-04 13:48:38 字数 1425 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-11-11 13:48:38

看来您的代码中可能存在错误。我认为这是正在发生的事情:

  1. 您单击第一个骰子,这将导致调用 selectDice,传入 diceName 的 id 和 diceValue 的值;或分别为“diceOne”和“0”。
  2. 无论 if 语句遵循哪个路径,您都会执行 document.getElementById(diceName),这将获取您单击的输入元素(因此在本例中为第一个输入元素),并将值更改为“现在已选择模具”或“未选择模具”。这就是一切出错的地方。
  3. 下次您单击该骰子时,它会调用 selectDice,并再次传入 diceName 的 id 和 diceValue 的值...除了这一次,diceName 是“diceOne”,diceValue 是“Die now selected”。由于 diceArray 对象中没有索引“Die now selected”的值,因此一切都从这里开始走下坡路。

我建议在没有“document.getElementById(diceName).value = ...”行的情况下尝试此代码,然后看看它是否有效。如果您确实需要更改 onclick 中的输入元素的值,那么我建议您不要使用内联 onclick,或者传入 diceName 和 diceValue 所需的文字值,如下所示:

<input type="button" id="diceOne" onclick="selectDice('diceOne','0')" value="0"></input>    

It looks like you might have a bug in your code. I think this is what is going on:

  1. You click on the first die, which leads to a call to selectDice, passing in the id for diceName, and the value for diceValue; or "diceOne" and "0" respectively.
  2. In whichever path of the if statement it follows, you do a document.getElementById(diceName), which is going to get the input element you clicked on (so in this case, the first input element), and changes the value to either "Die now selected" or "Die not selected". This is where everything goes wrong.
  3. The next time you click on that die, it calls selectDice, and again passes in the id for diceName, and the value for diceValue...except this time, diceName is "diceOne" and diceValue is "Die now selected". Since you don't have a value for the index "Die now selected" in the diceArray object, everything goes downhill from here.

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:

<input type="button" id="diceOne" onclick="selectDice('diceOne','0')" value="0"></input>    
脸赞 2024-11-11 13:48:38

为每个按钮创建变量,并为 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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文