Javascript 从计算出的总数中减去一个整数
我已经构建了一个脚本来添加数量/单位,并生成显示销售税的总额 在计算和添加商品及服务税(10%销售税)之前,我如何让这个计算器识别#折扣并从总额中减去它 此外,是否可以在页面加载时生成总计?而不是用户必须按下“生成总计”按钮 HTMLJavascript 从计算出的总数中减去一个整数,javascript,math,Javascript,Math,我已经构建了一个脚本来添加数量/单位,并生成显示销售税的总额 在计算和添加商品及服务税(10%销售税)之前,我如何让这个计算器识别#折扣并从总额中减去它 此外,是否可以在页面加载时生成总计?而不是用户必须按下“生成总计”按钮 HTML 项目1(单位) 项目2(单位) 项目3(单位) 折扣 总单位: 小计: 包括商品及服务税: 总数: JS 函数总数(){ var总价值=0; var all_others=document.getElementsByTagName(“输入”); 对于(va
- 项目1(单位)
- 项目2(单位)
- 项目3(单位)
折扣
总单位:
小计:
包括商品及服务税:
总数:
JS
函数总数(){
var总价值=0;
var all_others=document.getElementsByTagName(“输入”);
对于(var i=0;i首先,我建议您执行服务器端和客户端的验证和计算。第一个确保了安全性,而第二个提高了UI的响应能力
也就是说,您最好引入几个支持变量并对其执行计算。您应该使用getElementById
从感兴趣的元素中获取值,并将其存储在变量中。
然后,您应该对这些变量执行计算,并最终将结果放入要用于向用户显示它们的元素中
要在页面加载时执行操作,请查看。首先,要在窗口加载时执行函数,请将其包装在加载事件中:
window.onload = function() {
total();
}
其次,要使其在折扣中体现出来,只需修改变量几次,但在将它们添加到一起时,请确保使用.parseFloat()
:
function total(){
var total_value = 0;
var all_others = document.getElementsByTagName("input");
for(var i=0; i<all_others.length; i++){
if(all_others[i].type!="text" || all_others[i].name!="others"){
continue;
}
total_value += parseFloat(all_others[i].value);
}
document.getElementById("units_total").value = (total_value).toFixed(1);
document.getElementById("sub_total").value = ((total_value) *100).toFixed(2);
document.getElementById("g_total").value = (((total_value * 10/100) + total_value) * 100).toFixed(2);
document.getElementById("gst_total").value = ((total_value * 10/100) * 100).toFixed(2);
}
window.onload = function() {
total();
}
if (document.getElementById('discount').value != '') {
var discount = document.getElementById('discount').value;
}
else {
var discount = 0;
}
var sub_total = (((total_value) * 100).toFixed(2) - discount).toFixed(2);
var gst = ((total_value * 10 / 100) * 100).toFixed(2);
document.getElementById("sub_total").value = sub_total;
document.getElementById("gst_total").value = gst;
document.getElementById("g_total").value = (parseFloat(sub_total) + parseFloat(gst)).toFixed(2);