Javascript 添加整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4820827/
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
Javascript adding integers
提问by CodeGuy
How do I add a simple integer to another integer in Javascript?
如何在 Javascript 中将一个简单的整数添加到另一个整数?
I'm getting NaN as the value for total.
我得到 NaN 作为总值。
<script type="text/javascript">
var total = 0;
document.getElementById("dds1").onkeyup = function() {
    total = total + parseInt(this.value,10);
    updateIt();
};
function updateIt() {
//tofixed(2)
    document.getElementById("mySpan").innerHTML = total;
}
But if I do the following:
但是,如果我执行以下操作:
total = parseInt(this.value,10);
then total has a value (an integer value).
那么 total 有一个值(一个整数值)。
回答by ?ime Vidas
The problem is that you execute the additionread the input value on every keyup. If the user, for instance, presses BACKSPACE to clear the input, the value will be an empty string, which will result in NaN after parseInt. And once you have NaN (in your totalvariable), you cannot get rid of it anymore.
问题是您执行加法读取每个键上的输入值。例如,如果用户按 BACKSPACE 清除输入,则该值将是一个空字符串,这将在 parseInt 之后导致 NaN。一旦你有了 NaN(在你的total变量中),你就不能再摆脱它了。
Try this:
试试这个:
document.getElementById('dds1').onkeyup = function() {
    var value = parseInt(this.value, 10);
    if ( !isNaN(value) ) {
        total += value;
        updateIt();    
    }
};
Here, you first check if the input value can be parsed as a number. If not, you just disregard it.
在这里,您首先检查输入值是否可以解析为数字。如果没有,你就无视它。
Another way of doing it would be this:
另一种方法是这样的:
document.getElementById('dds1').onkeyup = function() {
    var value = parseInt(this.value, 10);
    isNaN(value) && return;
    total += value;
    updateIt();
};
Here, if you read an input value that cannot be converted into an number, you just return the function altogether.
在这里,如果您读取一个无法转换为数字的输入值,您只需完全返回该函数。
回答by Imtiyaz
Here is the javascript to add integers. Good thing is that it won't throw any error even for blank spaces.
这是添加整数的javascript。好消息是,即使对于空格,它也不会抛出任何错误。
Javascript
Javascript
<script language="javascript" type="text/javascript">
  function Add() {
    var a, b, c, d;
    a = parseInt(document.getElementById("txtFirstValue").value);
    //
    // If textbox value is null i.e empty, then the below mentioned if condition will 
    // come into picture and make the value to '0' to avoid errors.
    //
    if (isNaN(a) == true) { a = 0; }
    var b = parseInt(document.getElementById("txtSecondValue").value);
    if (isNaN(b) == true) { b = 0; }
    var c = parseInt(document.getElementById("txtThirdValue").value);
    if (isNaN(c) == true) { c = 0; }
    var d = parseInt(document.getElementById("txtFourthValue").value);
    if (isNaN(d) == true) { d = 0; }
    document.getElementById("txtTotal").value = a + b + c + d;
}
</script>
<!-- begin snippet: js hide: false -->
HTMLFirst Value: <asp:TextBox ID="txtFirstValue" runat="server"
                         onKeyUp="javascript:Add();"></asp:TextBox>
Second Value:<asp:TextBox ID="txtSecondValue" runat="server"
                         onKeyUp="javascript:Add();"></asp:TextBox>
Third Value:<asp:TextBox ID="txtThirdValue" rrunat="server" 
                         onKeyUp="javascript:Add();"><asp:TextBox>
Fourth Value:<asp:TextBox ID="txtFourthValue" runat="server" 
                         onKeyUp="javascript:Add();"></asp:TextBox>
Total = <asp:TextBox ID="txtTotal" runat="server" MaxLength="20" BackColor="#FFE0C0" 
                         Enabled="False" Font- Font-Bold="True" Font-Size="X-Large"> 
            </asp:TextBox>Referred from : http://www.ittutorials.in/source/javascript/scf4/addition-of-multiple-integers-using-javascript.aspx
引用自:http: //www.ittutorials.in/source/javascript/scf4/addition-of-multiple-integers-using-javascript.aspx

