简体   繁体   English

如何在JavaScript中使用输入文字的值

[英]How to use a value of an input text in javascript

I'm building a functionality on my "pig game", so the user can set himself the winning value if he wish, by entering it in an input field. 我正在“猪游戏”上构建功能,因此用户可以根据需要通过在输入字段中输入赢值来自己设置赢值。 I need to use the value the user enter in an <input type="text"> , in order to change a behavior in my JavaScript code. 我需要使用用户在<input type="text">的值来更改JavaScript代码中的行为。

I wrote my input as : 我写的输入为:

<div class ="newInputScore">
    <label for="fixGoal">SET THE GOAL :</label><br>
    <input name = "fixGoal" type = "text" id = "fixGoal">
</div>

and I need to use the value the user write in the above input, in my JS code. 我需要在JS代码中使用用户在上述输入中编写的值。 So far, i tried : 到目前为止,我尝试过:

let choosenScore = parseInt(document.getElementById('fixGoal').value);
if (scores[activePlayer] >= 100 || scores[activePlayer] >= choosenScore){...

... but it seems like my input is returning me a value of 0, because the player is actually winning with a score > 0 with the above code. ...但是似乎我的输入返回的值为0,因为上面的代码实际上是玩家得分> 0获胜。 I console log()'ed choosenScore and it returns me NaN, then undefined on the line beelow. 我控制台log()的choicenScore,它返回NaN,然后​​在下面的行中未定义。 What am I missing there? 我在那里想念什么? Thanks in advance. 提前致谢。

link to the project (dices dont appears because files are on my computer) : https://codepen.io/Peyo5202/pen/dybqyXV 链接到项目(由于文件在我的计算机上,所以未显示索引): https : //codepen.io/Peyo5202/pen/dybqyXV

Please provide more details, as you can see, your code works... 请提供更多详细信息,如您所见,您的代码有效...

 let choosenScore = parseInt(document.getElementById('fixGoal').value); 
 <div class ="newInputScore"> <label for="fixGoal">SET THE GOAL :</label><br> <input name = "fixGoal" type = "text" id = "fixGoal"> </div> <button onclick="console.log(parseInt(document.getElementById('fixGoal').value))">Start</button> 

There is type coercion (attempt to convert variables to same type) when you try to compare two variables that are not of the same type with javascript comparison operators - in your case, undefined converts to NaN and 当您尝试使用javascript比较运算符比较两个不同类型的变量时,会出现类型强制(试图将变量转换为相同类型)-在您的情况下, undefined转换为NaN

...with the caveat that any comparison involving NaN evaluates to false ...需要注意的是,涉及NaN任何比较都将评估为false

see this post to get a full explanation of this. 看到这篇文章以获得对此的完整解释。 Finally, false converts to +0 . 最后, false转换为+0

Suggestion: 建议:

Why not just put a default value of "0" for the input 为什么不将输入的默认值设置为"0"

 <div class ="newInputScore"> <label for="fixGoal">SET THE GOAL :</label><br> <input name = "fixGoal" type = "text" id = "fixGoal" value = "0"> </div> <button onclick="console.log(parseInt(document.getElementById('fixGoal').value))">Start</button> 

or better still, get the value of choosenScore with a ternary operator choosenScore ,使用三元运算符获取choosenScore的值

 function choosenScore() { let choosenScore = document.getElementById('fixGoal').value; return parseInt(choosenScore ? choosenScore : 0); } 
 <div class ="newInputScore"> <label for="fixGoal">SET THE GOAL :</label><br> <input name = "fixGoal" type = "text" id = "fixGoal"> </div> <button onclick="console.log(choosenScore())">Start</button> 

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

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