Javascript 如何在javascript中添加两个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12535285/
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 10:38:52  来源:igfitidea点击:

How to add two values in javascript

javascriptjavascript-events

提问by user1261388

This is a simple dropdown with values. I'm trying to pull the values as currency then add.

这是一个带有值的简单下拉列表。我试图将价值作为货币然后添加。

The values aren't being added (1+1=2or 1+2=3) but instead are concatenating (1+1=11or 1+2=12). Where am I going wrong here?:

这些值没有被添加(1+1=21+2=3),而是串联(1+1=111+2=12)。我哪里出错了?:

<script>
    function displayResult()
    {
        var strm=document.getElementById("matt").options[document.getElementById("matt").selectedIndex];
        var t=strm.text.search("\$");
        var strb=document.getElementById("box").options[document.getElementById("box").selectedIndex];
        var b=strb.text.search("\$");
        var x=strm.text.substr(t+1);
        var y=strb.text.substr(b+1);
        var math= x + y;

        alert(strm.text.substr(t+1));
        alert(strb.text.substr(b+1));
        alert(math);
    }
</script>

<form>
    Select #1:
    <select id="matt">
        <option>.00</option>
        <option>.00</option>
    </select>

    <br />
    Select #2:
    <select id="box">
        <option>.00</option>
        <option>.00</option>
    </select>

</form>

<button type="button" onclick="displayResult()">Display index</button>

回答by j08691

Use parseInt()to cast your strings as integers.

使用parseInt()投下你的字符串为整数。

var math= parseInt(x,10) + parseInt(y,10);

See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt

请参阅https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/parseInt

回答by laurent

To make sure your values are added as numbers, cast them to Numberfirst:

要确保您的值作为数字添加,请将它们转换为Number

Number('2') + Number('2') = 4
'2' + '2' = '22'

回答by Sushanth --

Try using parseFloat() or parseInt().. Otherwise it won't recognize it as a number.. It will append as a normal string..

尝试使用 parseFloat() 或 parseInt().. 否则它不会将其识别为数字.. 它将作为普通字符串附加..

var math= parseFloat(x) + parseFloat(y);

alert(math)

Check FIDDLE

检查小提琴

回答by Seth Flowers

The values are being interpreted as strings, you need to convert them to ints with parseInt prior to adding them.

这些值被解释为字符串,您需要在添加它们之前使用 parseInt 将它们转换为整数。

回答by David says reinstate Monica

The substring()function returns the substringof a given string, which is, of course, a string and not an object upon which math can be performed. If you parse those strings into a number, using parseInt(), then it will work:

substring()函数返回给定字符串的字符串,当然,它是一个字符串,而不是可以执行数学运算的对象。如果您将这些字符串解析为一个数字,使用parseInt(),那么它将起作用:

var x= parseInt(strm.text.substr(t+1),10);
var y= parseInt(strb.text.substr(b+1),10);

Also, you don't need to keep redeclaring var, you could instead comma-separated variable declarations:

此外,您不需要继续重新声明var,您可以改为使用逗号分隔的变量声明:

var x = parseInt(strm.text.substr(t+1),10),
    y = parseInt(strb.text.substr(b+1),10);

回答by user3619165

I discovered something interesting today. I was doing the calculator project on theodinproject. I have addition, subtraction, multiplication, and division functions. Subtraction, multiplication, and division works fine without needing to explicitly tell javascript to treat the parameter as a Number.

我今天发现了一个有趣的事情。我正在做 theodinproject 上的计算器项目。我有加法、减法、乘法和除法函数。减法、乘法和除法工作正常,无需明确告诉 javascript 将参数视为数字。