两个文本字段的总和 - javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:52:19  来源:igfitidea点击:

Sum of two textfields - javascript

javascriptsum

提问by Michiel

I found [this][1], rather difficult, javascript example online and I've implemented it with success in my website.

我在网上找到了 [this][1],相当困难的 javascript 示例,并且我已经在我的网站上成功实现了它。

However, I would like to get the result of, in this case, the two subtotals in one new text-field.

但是,在这种情况下,我想获得一个新文本字段中的两个小计的结果。

The traditional getElementbyIdand total.value=totaldidn't work out.

传统的getElementbyIdtotal.value=total没有奏效。

EDIT

编辑

 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);
}

This is the javascript I use. But for some reason, when I have just one value (twogig), the totalis set as NaN. What is wrong with my script?

这是我使用的javascript。但出于某种原因,当我只有一个值 ( twogig) 时,将total设置为 NaN。我的脚本有什么问题?

回答by John

Well, if you have assigned ID's to text inputs, for example: <input type="text" id="my_input" />, then you can call it with document.getElementById('my_input').value.

好吧,如果您已将 ID 分配给文本输入,例如:<input type="text" id="my_input" />,那么您可以使用 document.getElementById('my_input').value 调用它。

So:

所以:

<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>

Naturally, that is a very basic script, it doesn't check to make sure the entered values are numbers. We have to convert the fields into integers, otherwise they'll be strings (so 2+2 would equal 22). When the button is clicked, the function is called, which makes a variable for each input box, converts them to ints, adds them, and outputs our sum.

当然,这是一个非常基本的脚本,它不会检查以确保输入的值是数字。我们必须将字段转换为整数,否则它们将是字符串(因此 2+2 将等于 22)。当按钮被点击时,该函数被调用,它为每个输入框创建一个变量,将它们转换为整数,将它们相加,并输出我们的总和。

回答by SuperKael

Replace:

代替:

document.getElementById('total').value = parseInt(sum);

With:

和:

document.getElementById('total').value = sum;

You have already done all the int-parsing you need to do.

您已经完成了所有需要进行的 int 解析。

回答by gauravbhai daxini

$("#value1, #value2").on('focusout', function() {
  var value2 = parseInt($("#value2").val()) > 0 ? parseInt($("#value2").val()) : 0;
  var value1 = parseInt($("#value1").val()) > 0 ? parseInt($("#value1").val()) : 0
  var sumOfValues = value1 + value2;
  console.log('Your sum is ' + sumOfValues);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input" id="value1">
<input type="text" class="input" id="value2">