为什么将两个分数相加,它们以0.5的步长递增,总框是否只产生不带半分的整数?

问题描述 投票:0回答:1

两个分数以0.5的步长上升,但是当其中一个分数为例如时,总框仅产生整数。 3.5?任何原因,可以纠正吗?

<body>
<form action="trial.php" method="post" name="trial" target="_top">
<label>Score1<input id = "scorebox1" type="number" value="0" max="20" min="0" step="0.5" size="5" 
maxlength="5" oninput="totalscore()"/></label>
<label>Score 2<input id = "scorebox2" type="number" value="0" max="20" min="0" step="0.5" size="5" 
maxlength="5" oninput="totalscore()"/></label>
<label>Total<input id = "totals" type="number" value="0" step="0.5" size="5" maxlength="5" 
oninput="totalscore()" disabled="disabled"/></label>

</form>

<script>
function totalscore(){  

var totals =
parseInt(document.getElementById('scorebox1').value) +
parseInt(document.getElementById('scorebox2').value);

document.getElementById('totals').value = totals;
}
</script>
</body>
numbers integer decimal addition
1个回答
0
投票

我认为parseInt可能只处理Integer。通过将值加倍,然后在“总计”框中显示之前将其减半,可以得到正确的输出。因此,对Javascript的此修正给出了所需的内容。

<script>
 function totalscore(){ 

 var    totals =
 parseInt(document.getElementById('scorebox1').value*2) +
 parseInt(document.getElementById('scorebox2').value*2);

 document.getElementById('totals').value = totals/2;
 }
 </script>

我想如果处理货币,您可以乘以100,然后除以100,以使货币起作用。

© www.soinside.com 2019 - 2024. All rights reserved.