简体   繁体   English

在JavaScript中添加多个HTML表单输入值

[英]Adding multiple HTML form input values in JavaScript

I am bewildered as to why I cannot add three numbers together. 为什么我不能将三个数字加在一起,我感到困惑。 Here is my HTML: 这是我的HTML:

<div><label>Sales Price: </label>
    <input type="number" name="sales_price" value="3">
</div>
<div><label>Incentives: </label>
    <input type="number" name="incentives" value="2">
</div>    
<div><label>Acquisition Fee: </label>
    <input type="number" name="acq_fee" value="1">

Here is my JavaScript: 这是我的JavaScript:

var salesPrice = document.getElementsByName("sales_price")[0];
var incentives = document.getElementsByName("incentives")[0];
var acqFee = document.getElementsByName("acq_fee")[0];

var netCapCost = salesPrice.value - incentives.value + acqFee.value;

I wanted a simple calculation to be done: (3-2+1) = 2. However, netCapCost returns 11, which is the concatenation of the result of (3-2) and 1. What did I do wrong? 我想做一个简单的计算:(3-2 + 1)=2。但是, netCapCost返回11,这是(3-2)和1的结果的并置。我做错了什么? Many Thanks in advance! 提前谢谢了!

You need to convert those values into numbers with parseInt() or else the + operator will be interpreted as string concatenation. 您需要使用parseInt()将这些值转换为数字,否则+运算符将被解释为字符串连接。 You are doing 你在做

var netCapCost = salesPrice.value - incentives.value + acqFee.value;

Which is 哪一个

var netCapCost = "3" - "2" + "1"

"3"-"2" will return 1, which is what you want but 1 + "1" will be concatenated into "11" since the right operand is a string. "3"-"2"将返回1,这是您想要的,但是1 + "1"将被连接为"11"因为右操作数是一个字符串。 So Number + String -> concatenation 所以数字+字符串->串联

you are doing a string concatation, all value get from input value are string, 您正在执行字符串连接,所有从输入值获取的值都是字符串,

the first calcuation salesPrice.value - incentives.value is currect is becuase, the - sign convert the incentives.value to number 首先计算salesPrice.value - incentives.value .value是正确的,因为-号将promotions.value转换为数字

the currect way is 正确的方法是

var netCapCost = parseInt(salesPrice.value, 10) - parseInt(incentives.value, 10) + parseInt(acqFee.value, 10);

it's better to use a Math library to do the calcuation in javascript, because sooner or later, you will encounter the problem like 0.3 - 0.2 = 0.09999999 最好使用数学库在javascript中进行计算,因为迟早会遇到类似0.3 - 0.2 = 0.09999999

 var salesPrice; var incentives; var acqFee; var npc; function calculate(e) { var netCapCost = (parseFloat(salesPrice.value) - parseFloat(incentives.value) + parseFloat(acqFee.value)).toPrecision(3); npc.value = netCapCost; } window.onload = function (){ salesPrice = document.getElementsByName("sales_price")[0]; incentives = document.getElementsByName("incentives")[0]; acqFee = document.getElementsByName("acq_fee")[0]; npc = document.getElementsByName("npc")[0]; salesPrice.onchange = calculate; calculate(); }; 

Your problem is that text fields value is always of type STRING. 您的问题是文本字段的值始终为STRING类型。 When subtracting it forces a conversion to type FLOAT. 减去时将强制转换为FLOAT类型。 Then the plus operation shares an opperator with the concatenate operation. 然后加号操作与串联操作共享一个运算符。 So when you have two strings being added it concatenates rather than converting to FLOAT or INT. 因此,当您添加两个字符串时,它会串联在一起,而不是转换为FLOAT或INT。 So basically you have "2"-"1" being converted to 2-1 as strings cannot be subtracted which gives you (1) then you have (1)+"1" which will concatenate rather than add as you have a string. 因此,基本上,您有将“ 2”-“ 1”转换为2-1的原因,因为不能减去字符串,这会给您(1),那么您就有了(1)+“ 1”,它们会串联起来而不是加起来,因为您有字符串。 Always use parseFloat or parseInt when expecting numeric data from user entry as it will always be a string when originally submitted. 当期望来自用户输入的数字数据时,请始终使用parseFloat或parseInt,因为最初提交时它将始终是字符串。

I think confusion here is HTML 5 introduces input type number however javascript engine doesn't introduce support for reading such specific fields. 我认为这里的困惑是HTML 5引入了输入类型号,但是javascript引擎并未引入对读取此类特定字段的支持。 We end up using old traditional way of reading input field value which defaults everything to string. 我们最终使用了传统的读取输入字段值的传统方法,该方法将所有内容默认为字符串。

Only advantage of using number type field would be that you do not have to worry about exception/erroneous situation where number is not being entered. 使用数字类型字段的唯一好处是,您不必担心未输入数字的异常/错误情况。

Other answer suggesting to use parseInt function, is the way to go unless you have luxury of introducing javascript framework like jQuery and use more sophisticated approach for reading it. 建议使用parseInt函数的其他答案是一种方法,除非您有很多介绍jQuery之类的JavaScript框架并使用更复杂的方法来阅读它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM