JavaScript 添加整数

发布于 2024-10-14 19:02:34 字数 459 浏览 1 评论 0原文

如何在 Javascript 中将一个简单整数添加到另一个整数?

我得到 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;
}

但如果我执行以下操作:

total = parseInt(this.value,10);

那么总计就有一个值(整数值)。

How do I add a simple integer to another integer in Javascript?

I'm getting NaN as the value for total.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2024-10-21 19:02:35

问题是您执行加法在每次按键时读取输入值。例如,如果用户按 BACKSPACE 清除输入,则该值将是一个空字符串,这将在 parseInt 后导致 NaN。一旦你有了 NaN(在你的 total 变量中),你就无法再摆脱它了。

试试这个:

document.getElementById('dds1').onkeyup = function() {

    var value = parseInt(this.value, 10);

    if ( !isNaN(value) ) {
        total += value;
        updateIt();    
    }

};

在这里,您首先检查输入值是否可以解析为数字。如果没有,你就直接无视它。


另一种方法是这样的:

document.getElementById('dds1').onkeyup = function() {
    var value = parseInt(this.value, 10);

    isNaN(value) && return;

    total += value;
    updateIt();
};

在这里,如果您读取无法转换为数字的输入值,则只需完全返回该函数即可。

The problem is that you execute the addition read 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 total variable), you cannot get rid of it anymore.

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.

笑饮青盏花 2024-10-21 19:02:35

这是用于添加整数的 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 -->

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

引用自:http://www. ittutorials.in/source/javascript/scf4/addition-of-multiple-integers-using-javascript.aspx

Here is the javascript to add integers.
Good thing is that it won't throw any error even for blank spaces.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文