[英]How to get sum of input values?
hello im trying to add input totals to a grand total when any input is changed. 你好,我想在任何输入更改时将输入总计添加到总计中。 i keep getting NaN in the total box. 我一直在总盒子里得到NaN。 here is my code 这是我的代码
<div class="col-md-6">
<input type="text" placeholder="Description" class="form-control" id="d" name="description" />
</div>
<div class="col-md-1">
<input type="text" placeholder="price." class="form-control" id="q" name="itemprice"/>
</div>
<div class="col-md-1">
<input type="text" placeholder="price" class="form-control" id="itemprice" name="itemprice1"/>
</div>
<div class="col-md-2">
<input type="text" placeholder="price" class="form-control" id="itemprice" name="itemprice2" onchange="myFunction()"/>
<td>Subtotal</td>
<td class="total" id="tot" for="tot">
<input type="total" id="total">
<script type="text/javascript">
function myFunction()
{
var answer = document.getElementById('total');
var x = document.getElementsByName('itemprice');
var y = document.getElementsByName('itemprice1');
var z = document.getElementsByName('itemprice2');
var d = document.getElementsByName('itemprice3');
answer.value = x.value + y.value + z.value + d.value;
}
</script>
document.getElementsByName
returns a nodelist (because multiple nodes can have the same name). document.getElementsByName
返回一个节点列表(因为多个节点可以具有相同的名称)。 (That's why the name contains Elements
with an 's' as opposed to getElementById
, which returns 1 element at most. (这就是为什么名称包含带有's'的Elements
,而getElementById
最多返回1个元素的原因。
Such a list doesn't have a value. 这样的列表没有价值。 You would either get the value of each element, or if you know it will always be one, get the value of the first element: 您可以获取每个元素的值,或者如果知道它始终为1,则获取第一个元素的值:
var y = document.getElementsByName('itemprice1')[0].value
But note, you don't have any elements that have the name itemprice3
, so document.getElementsByName('itemprice3')
will return a nodelist with 0 items. 但请注意,您没有任何名称为itemprice3
元素,因此document.getElementsByName('itemprice3')
将返回包含0个项目的节点列表。 The line above will fail in that case. 在这种情况下,上面的行将失败。
Also note, you have two elements with id itemprice
, which is illegal. 另请注意,您有两个ID为itemprice
元素,这是非法的。
When that works, you will need parseInt()
to convert the values to integers. 在这种情况下,您将需要parseInt()
将值转换为整数。
answer.value = parseInt(x.value) + parseInt(y.value) + parseInt(z.value) + parseInt(d.value);
If the inputs can contain floating point values as well, use parseFloat
instead of parseInt
. 如果输入也可以包含浮点值,请使用parseFloat
代替parseInt
。
See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt 请参阅: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/parseInt
Use Id's (fix duplicate id, remove reference to itemprice3 as it doesn't exist in the HTML). 使用ID(修正重复ID,删除对itemprice3的引用,因为它在HTML中不存在)。 Convert text values to numbers before adding. 在添加之前将文本值转换为数字。
<div class="col-md-6">
<input type="text" placeholder="Description" class="form-control" id="d" name="description" />
</div>
<div class="col-md-1">
<input type="text" placeholder="price." class="form-control" id="itemprice" name="itemprice"/>
</div>
<div class="col-md-1">
<input type="text" placeholder="price" class="form-control" id="itemprice1" name="itemprice1"/>
</div>
<div class="col-md-2">
<input type="text" placeholder="price" class="form-control" id="itemprice2" name="itemprice2" onchange="myFunction()"/>
<td>Subtotal</td>
<td class="total" id="tot" for="tot">
<input type="total" id="total">
<script type="text/javascript">
function myFunction()
{
var answer = document.getElementById('total');
var x = document.getElementById('itemprice');
var y = document.getElementById('itemprice1');
var z = document.getElementById('itemprice2');
//var d = document.getElementsById('itemprice3');
// parseFloat converts to values, otherwise you'll concatenate the strings.
answer.value = parseFloat( "0" + x.value ) + parseFloat("0" + y.value) + parseFloat("0" + z.value); // + d.value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.