我的一个javascript函数遇到了问题。我试图将整数除以整数,并继续得到NaN作为结果。我已阅读并尝试了各种可能的解决方案,但一直无法找到正确的答案。请查看以下代码(如果需要,请加载它),如果可以,请告诉我如何修复它。感谢
<!doctype html>
<html>
<style>
*:focus {
outline: none;
}
@font-face {
font-family: systems_analysis;
src: url(systems_analysis.ttf);
}
@font-face {
font-family: eufont;
src: url('eufont.ttf');
}
.button {
background-color: white;
color: black;
border-radius: 6px;
border: none;
cursor: pointer;
width: 400px;
height: 20px;
font-size: 15px;
font-family: systems_analysis;
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.2), 0 1.5px 5px 0 rgba(0,0,0,0.19);
}
.button:hover {
background-color: black;
color: white;
}
.button:active {
transform: translateY(1px);
}
.button:focus {
outline:0 !important;
}
.balanceBox {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 400px;
}
.priceBoxAltra {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 225px;
}
.priceBoxBexa {
background-color: transparent;
color: white;
font-family: arial;
font-size: 40px;
border: 1px transparent;
text-align: center;
width: 225px;
}
body {
background-image: url("risefx_background.jpg");
}
</style>
<head>
<title>RiseFX</title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center>
<span><input type="text" id="altraPrice" class="priceBoxAltra" value="1.00000"></span>
<span><input type="text" id="bexaPrice" class="priceBoxBexa" value="1.00000"></span>
</center>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<center>
<input type="text" id="fxPrice" class="priceBoxAltra" value="0.00000">
</center>
<br>
<center>
<span><input type="button" id="altraRoll" class="button" value="TRADE" onclick="altraFunction()"></span>
<span><input type="button" id="bexaRoll" class="button" value="TRADE" onclick="bexaFunction()"></span>
</center>
<br>
<br>
<center>
<input type="text" id="balance" class="balanceBox" value="1.0000000000" readonly>
</center>
<script>
function altraFunction() {
var altraRollVar = (Math.random()*2);
document.getElementById("altraPrice").value = altraRollVar.toFixed(5);
fxPriceConversion();
}
</script>
<script>
function bexaFunction() {
var bexaRollVar = (Math.random()*2);
document.getElementById("bexaPrice").value = bexaRollVar.toFixed(5);
fxPriceConversion();
}
</script>
<script>
function fxPriceConversion() {
var altraRollBoxVar = document.getElementById("altraRoll").value;
var bexaRollBoxVar = document.getElementById("bexaRoll").value;
var conversion = altraRollBoxVar / bexaRollBoxVar;
document.getElementById("fxPrice").value = conversion.toFixed(5);
}
</script>
</body>
</html>
答案 0 :(得分:2)
首先,请阅读有关如何创建Minimal, Complete, and Verifiable Example的信息。这里有很多混乱。
其次,你的问题非常明显。你已经完全隔离了这个问题,你知道它与分裂有关。你唯一的部门是这些部门:
var altraRollBoxVar = document.getElementById("altraRoll").value;
var bexaRollBoxVar = document.getElementById("bexaRoll").value;
var conversion = altraRollBoxVar / bexaRollBoxVar;
bexaRollBoxVar
的值是ID为bexaRoll
的输入值。该值为"TRADE"
。你的代码都没有改变过。所以,上面的第三行除以字符串,而不是整数。因此错误。
我应该指出altraRollBoxVar
存在同样的问题。您实际上是将字符串除以字符串。不出所料,这不起作用。
即使是调试此问题的最小尝试,例如alert(bexaRollBoxVar);
,也会显示问题。将来,请在发布此处之前自行解决问题。