我是一名学生,并努力让我的javascript将增值税添加到另一个'变量'
有人可以协助 - 大桶显示为" 0"
function calculateTotal(){
var total = 0;
var vat = total*0.14;
for (var k = 0; k < document.forms.service.length; k++)
{
if(document.forms.service.elements[k].checked){
total+=Number(document.forms.service.elements[k].value);
}
}
document.getElementById("total").innerHTML= "The total is R " + total;
document.getElementById("vat").innerHTML= "The total VAT is R " + vat;
}
答案 0 :(得分:4)
将var vat = total*0.14;
移至功能的末尾。计算总数后,您必须计算增值税。
function calculateTotal(){
var total = 0;
for (var k = 0; k < document.forms.service.length; k++)
{
if(document.forms.service.elements[k].checked){
total+=Number(document.forms.service.elements[k].value);
}
}
var vat = total*0.14;
document.getElementById("total").innerHTML= "The total is R " + total;
document.getElementById("vat").innerHTML= "The total VAT is R " + vat;
}