function doMath()
{
// Capture the entered values of two input boxes
var twogiga = document.getElementById('twogig').value;
var fourgiga = document.getElementById('fourgig').value;
// Add them together and display
var sum = parseInt(twogiga) + parseInt(fourgiga);
document.getElementById('total').value = parseInt(sum);
}
这是我使用的 JavaScript。但由于某种原因,当我只有一个值时(twogig), the total设置为 NaN。我的脚本有什么问题吗?
<input type="text" id="my_input1" />
<input type="text" id="my_input2" />
<input type="button" value="Add Them Together" onclick="doMath();" />
<script type="text/javascript">
function doMath()
{
// Capture the entered values of two input boxes
var my_input1 = document.getElementById('my_input1').value;
var my_input2 = document.getElementById('my_input2').value;
// Add them together and display
var sum = parseInt(my_input1) + parseInt(my_input2);
document.write(sum);
}
</script>