javascript - 多个带有数字的变量并将结果再次添加到变量中返回错误的结果
问题描述
function calculateAmount(val) {
var price = val;
if (quantity >= 100 && quantity < 1000) {
var divobj = document.getElementById('discount');
divobj.value = 20;
var divobj1 = document.getElementById('total');
var total = ((20 / 100) * parseInt(price)) + parseInt(price);
divobj1.value = total;
}
}
例如,如果输入数量为 100,则输出返回为 10020 而不是 100+20 = 120
解决方案
var price = val;
因为 val 是一个字符串:
var price = +val;
请测试此代码,我希望此代码有效
divobj.value = 20;
请删除上面的行代码:
var total = ((divobj.value / 100) * parseInt(price)) + parseInt(price);
并使用上面的代码而不是下面的代码:
var total = ((20 / 100) * parseInt(price)) + parseInt(price);
推荐阅读
- python - python selenium 输入搜索查询然后等待
- sql - 加入表并根据列中的值维护相同数量的行
- jquery - JQuery DataTables - ServerSide JSON - 如何在有多个页面时填充 columnFilter 数据
- php - 使用 PHP 从多个数组项生成组合
- hadoop - 如何从 HDFS 的目录结构中删除所有文件?
- java - 如何 putExtra 和 getExra 来自任何其他活动的字符串?像一个全局变量?
- gnuplot - gnuplot 设置链接和设置非线性
- r - 传播 data.frame 并添加列
- ios - Swift 4 - 音板应用程序 - 以编程方式将多个按钮标签分配给一个按钮?
- components - 如何在 CDE Pentaho 中创建动态标题列?