在 JavaScript 中实例化文本字段值之外的变量 [英] Instantiate a variable out of text field value in JavaScript
问题描述
因此,我尝试了许多关于如何从输入文本字段获取值并将其存储在全局变量中的不同建议.我正在开发这个游戏,它需要从分数文本字段中获取值并为二十一点风格的游戏添加随机数.除了分数变量之外,我的一切都在工作.这看起来很简单,但我无法弄清楚为什么变量不会存储和/或更新值.如果有人能帮助我,那就太棒了.
So I have tried many different suggestions on how to get a value from an input text field and store it in a global variable. I am working on this game and it needs to take the value from the score text field and add up random numbers for a black jack style game. I have everything working except for the score variable. This seems so simple yet I can't figure out why the variable will not store and/or update the value. If some one can help me out that would be awesome.
这是我的代码:
<html>
<head>
<title>Project 3</title>
<style type="text/css">
body { background:BlanchedAlmond; color:DarkOrange; font-family: sans-serif; font-weight:bold }
input { text-align:center; background:DarkOrange; color:RoyalBlue; font-weight:bold }
td { margin:0; padding:10 }
</style>
<script language="JavaScript">
function doFunction(ref)
{
score =document.getElementById("1").value;
if (ref.value== "X")
{
randnum=Math.floor( 9*Math.random() ) + 1;
ref.value=randnum;
score = score + ref.value;
}
if(score > 21)
{
window.alert ("You hit " + ref.value + " You have lost");
}
if(score == 21)
{
window.alert ("Congradulations! You hit 21 and have won the game!");
}
}
</script>
</head>
<body>
<center>
<h3>Uncover 21</h3>
<form name = "myform" >
<TABLE>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
<TR>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
<TD><input id="button" type="button" value="X" onclick="doFunction(this)" /></TD>
</TR>
</TABLE>
Your Score <input id="1" type="text" name="Score" value="0" readonly /><br>
<input id="resetbutton" type="reset" value ="Restart Game" onclick="document.location.reload()" />
</form>
</center>
</body>
</html>
推荐答案
您没有更新分数字段.试试这个
you are not updating the score field.try this
function doFunction(ref)
{
score =document.getElementById("1").value;
if (ref.value== "X")
{
randnum=Math.floor( 9*Math.random() ) + 1;
ref.value=randnum;
score = parseInt(score + ref.value, 10);
document.getElementById("1").value = score;
}
}
您还必须将第一个警报更改为
also you have to change the first alert to
if(score > 21)
{
window.alert ("You hit " + score + " You have lost");
}
这篇关于在 JavaScript 中实例化文本字段值之外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!