Javascript - 返回连接而不是输入变量的总和
Javascript - returning concatenates instead of sum of variables from input
我正在尝试使用 .each()
从输入字段中获取值并将它们相加以在另一个输入值中显示总和。但不幸的是,尽管使用了 parseInt() 我还是无法得到总和。
HTML
<input type=text name=value[] id=value class="value">
<input type=text name=value[] id=value class="value">
<input type=text name=totalvalue id=totalvalue>
JavaScript
var totalvalue = parseInt(0);
$(document).on("focusout",".value",function() {
$(".value").each(function() {
totalvalue = parseInt(totalvalue + $(this).val());
});
$("#totalvalue").val(totalvalue);
});
尝试 parseFloat()
输入值
parseFloat($(this).val()));
var totalvalue = parseInt(0);
$(document).on("focusout",".value",function() {
$(".value").each(function() {
totalvalue = (totalvalue + parseFloat($(this).val()));
});
$("#totalvalue").val(totalvalue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=text name=value[] id=value class="value">
<input type=text name=totalvalue id=totalvalue>
你正在解析的行是说
Add the totalValue
to the string from the input, then parse the result.
这就是它串联的原因。相反,您需要添加 parseInt()
的 totalValue
outside 以便它说
Take the take the totalValue
and add the parsed input value to it.
像这样
totalvalue = totalvalue + parseInt($(this).val());
这可以进一步缩短为
totalvalue += parseInt($(this).val());
您的附加字符串。 $(this).val()
是一个字符串。只需将其解析为 int 或 float。
如果根据您的代码,您的目的是这样,那么它将始终与第一个框值相加。
var value = parseInt($(this).val());
if(value)
totalvalue = totalvalue + value;
});
否则你应该详细说明你的问题,以便提供正确的答案。
您正在将 NaN 数据类型添加到 Number 数据类型。所以你需要在执行你的加法操作之前限制它。
请勾选这个
var totalvalue = 0;
$(document).on("focusout",".value",function() {
$(".value").each(function() {
if(isNaN($(this).val()) || $(this).val() != ''){
totalvalue = totalvalue + parseInt($(this).val());
}
});
$("#totalvalue").val(totalvalue);
});
你应该在 focusout 函数里面声明 totalvalue
,因为它总是显示两个输入文本的总和,并且在求和时传递输入值空状态!!!
$(document).on("focusout",".value",function() {
var totalvalue = 0;
$(".value").each(function() {
if($(this).val() == "") return;
totalvalue += parseInt($(this).val());
});
$("#totalvalue").val(totalvalue);
});
我正在尝试使用 .each()
从输入字段中获取值并将它们相加以在另一个输入值中显示总和。但不幸的是,尽管使用了 parseInt() 我还是无法得到总和。
HTML
<input type=text name=value[] id=value class="value">
<input type=text name=value[] id=value class="value">
<input type=text name=totalvalue id=totalvalue>
JavaScript
var totalvalue = parseInt(0);
$(document).on("focusout",".value",function() {
$(".value").each(function() {
totalvalue = parseInt(totalvalue + $(this).val());
});
$("#totalvalue").val(totalvalue);
});
尝试 parseFloat()
输入值
parseFloat($(this).val()));
var totalvalue = parseInt(0);
$(document).on("focusout",".value",function() {
$(".value").each(function() {
totalvalue = (totalvalue + parseFloat($(this).val()));
});
$("#totalvalue").val(totalvalue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type=text name=value[] id=value class="value">
<input type=text name=totalvalue id=totalvalue>
你正在解析的行是说
Add the
totalValue
to the string from the input, then parse the result.
这就是它串联的原因。相反,您需要添加 parseInt()
的 totalValue
outside 以便它说
Take the take the
totalValue
and add the parsed input value to it.
像这样
totalvalue = totalvalue + parseInt($(this).val());
这可以进一步缩短为
totalvalue += parseInt($(this).val());
您的附加字符串。 $(this).val()
是一个字符串。只需将其解析为 int 或 float。
如果根据您的代码,您的目的是这样,那么它将始终与第一个框值相加。
var value = parseInt($(this).val());
if(value)
totalvalue = totalvalue + value;
});
否则你应该详细说明你的问题,以便提供正确的答案。
您正在将 NaN 数据类型添加到 Number 数据类型。所以你需要在执行你的加法操作之前限制它。
请勾选这个
var totalvalue = 0;
$(document).on("focusout",".value",function() {
$(".value").each(function() {
if(isNaN($(this).val()) || $(this).val() != ''){
totalvalue = totalvalue + parseInt($(this).val());
}
});
$("#totalvalue").val(totalvalue);
});
你应该在 focusout 函数里面声明 totalvalue
,因为它总是显示两个输入文本的总和,并且在求和时传递输入值空状态!!!
$(document).on("focusout",".value",function() {
var totalvalue = 0;
$(".value").each(function() {
if($(this).val() == "") return;
totalvalue += parseInt($(this).val());
});
$("#totalvalue").val(totalvalue);
});