jquery、ajax数据求和无法正常工作
$.post('ajax_ceneizbaze.php', function(cenovnik){
if(cenovnik){
cenastr=cenovnik.cenastrana;
cenadinamika=cenovnik.cenadinamika;
cenabaza=cenovnik.cenabaza;
cenakorpa = cenovnik.cenakorpa;
cenacms = cenovnik.cenacms;
inkrementodrzavanje = cenovnik.cenaodrzavanje;
rezz = parseInt(cenastr+cenadinamika);
alert(rezz);
}
else alert('bla bla..');
},'json');
cenastr 的初始值为 25,cenadinamika 的初始值为 50,Ajax 在这个我的示例中完美运行,但是当我尝试对 cenastr 和 cenadinamika 的值求和时,我得到输出 2550,而不是 75?为什么我不能将其转换为整数并获得这两者的总和。它只以字符串格式输出结果。我尝试将 parseInt 放置在求和操作之前,但没有帮助。
$.post('ajax_ceneizbaze.php', function(cenovnik){
if(cenovnik){
cenastr=cenovnik.cenastrana;
cenadinamika=cenovnik.cenadinamika;
cenabaza=cenovnik.cenabaza;
cenakorpa = cenovnik.cenakorpa;
cenacms = cenovnik.cenacms;
inkrementodrzavanje = cenovnik.cenaodrzavanje;
rezz = parseInt(cenastr+cenadinamika);
alert(rezz);
}
else alert('bla bla..');
},'json');
initial value for cenastr is 25, and for cenadinamika is 50 ,Ajax works perfectly in this mine example, but when i try to sum values cenastr and cenadinamika i get output 2550 , instead 75? why i cant convert that to integer and to get sum of thoose two. it only output result in string format. i tried parseInt to place before sum operation but it doesnt helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你必须解析每个字符串:
尝试一下
you have to parseInt each of the strings:
Try that out
http://www.javascripter.net/faq/convert2.htm - 这可能有帮助。在计算之前需要将字符串转换为数字!
http://www.javascripter.net/faq/convert2.htm - this might help. You need to convert the strings to numbers before the calculation!
+
运算符有双重用途。对于字符串,它将它们连接起来:“25”+“50”=“2550”
对于数字,它将它们相加。
25 + 50 = 75
因此,我们可以推断出您的两个变量是字符串,并且您解析将它们连接为整数的结果,得到 2550。
在使用
+ 之前,您需要将每个单独的值解析为 int
运算符来添加它们:The
+
operator has a dual purpose. On strings it concatenates them:"25" + "50" = "2550"
With numbers, it sums them.
25 + 50 = 75
Therefore we can deduce that your two variables are strings, and that you parse the result of concatenating them to an integer, giving you 2550.
You need to parse each individual value to an int before using the
+
operator to add them:在添加之前确保变量是数字:
Make sure the variable are numbers before adding:
parseInt 将处理加法的结果,两者都是字符串,将是连接。
要么:
要么使用一元运算符:
parseInt will be working on the result of the addition, which, both being strings, will be the concatenation.
Either:
or use the unary operator: