简体   繁体   English

如何根据在文本字段中输入的不匹配数字使 div 元素内的数字减少?

[英]How do I get a number inside a div element to decrement based on an non matching number entered in text field?

I wanted to first say that I am new as new can get to javascript.我想首先说我是新手,因为新手可以使用 javascript。 I am trying to do a remake of the number guessing game where I have a div with a number in it that I want decreased when I guess wrong.我正在尝试重新制作数字猜谜游戏,其中我有一个 div,其中有一个数字,当我猜错时我想减少它。 I have been able to get the game to print out a message saying whether I guessed right or wrong, but I can't get it to decrement the number in the other div box I created.我已经能够让游戏打印出一条消息,说明我猜对了还是错了,但我不能让它减少我创建的另一个 div 框中的数字。 I tried to use parseInt, but I feel that I'm so new that I am way off.我尝试使用 parseInt,但我觉得我太新了,以至于我很遥远。 I had already pre-assigned a number inside the div, but I'm assuming that it is in string form and must somehow be converted before I can even decrease it.我已经在 div 中预先分配了一个数字,但我假设它是字符串形式的,并且必须以某种方式进行转换,然后才能减少它。

I have a simple text field(for entering the guessed number attempt), a message field above it in a div, a div below that shows the number I guessed after entered, and a div with guesses left.我有一个简单的文本字段(用于输入猜测的数字尝试),它上方的 div 中的消息字段,下方的 div 显示我输入后猜测的数字,还有一个带有猜测的 div。 I gave up and deleted the code, any help would be appreciated.我放弃并删除了代码,任何帮助将不胜感激。 I just want to know how to decrease that guesses left number from 10 to below when I've guessed wrong.当我猜错时,我只想知道如何将猜测的左数从 10 减少到以下。

When you submit your guess number in your form, you'll need to update the "guesses remaining" value.当您在表单中提交您的猜数时,您需要更新“猜数剩余”值。 Just have your HTML display the value.toString() and update the value in JS using a click handler on your form ( https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick ).只需让您的 HTML 显示 value.toString() 并使用表单上的单击处理程序更新 JS 中的值( https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onclick )。 This should get you moving in the right direction.这应该让你朝着正确的方向前进。

That form submission should also validate your input answer btw.该表单提交还应该验证您的输入答案顺便说一句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM