我正在创建一个简单的Web应用程序,该应用程序使用两个值,输出总计,然后在VAT字段中显示20%的VAT(为澄清起见,这显示了先前计算的总计20%),然后应该有一个最终值总计字段,将总计添加到增值税中,但是由于我很难在代码中计算增值税,因此我没有对此部分进行四舍五入,这是到目前为止的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form>
Value 1: <input type="text" name="val1"><br>
Value 2: <input type="text" name="val2"><br>
Total: <input type="text" name="sum"><br>
VAT:<input type="text" name="vat"><br>
<input type="button" value="Sum" onclick="sumTotal() + vatCalc()">
</form>
<script>
function sumTotal() {
let val1 = document.getElementsByName("val1")[0].value;
let val2 = document.getElementsByName("val2")[0].value;
let sum = Number(val1) + Number(val2);
document.getElementsByName("sum")[0].value = sum;
}
function vatCalc () {
// let vat= document.getElementsByName("vat")
var sumVat = 20;
let vat = (sum/100)*(sumVat+100);
document.getElementsByName("vat")[0].value = vat;
}
</script>
</body>
</html>
任何帮助将不胜感激,谢谢!
答案 0 :(得分:-1)
我已经编辑了您的函数,因此sumtotal可以调用您的vat函数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form>
Value 1: <input type="text" name="val1"><br>
Value 2: <input type="text" name="val2"><br>
Total: <input type="text" name="sum"><br>
VAT:<input type="text" name="vat"><br>
<input type="button" value="Sum" onclick="sumTotal()">
</form>
<script>
function sumTotal() {
let val1 = document.getElementsByName("val1")[0].value;
let val2 = document.getElementsByName("val2")[0].value;
let sum = Number(val1) + Number(val2);
document.getElementsByName("sum")[0].value = sum;
vatCalc(sum);
}
function vatCalc (sum) {
// let vat= document.getElementsByName("vat")
var sumVat =0.20;
let vat = sumVat*sum;
document.getElementsByName("vat")[0].value = vat;
}
</script>
</body>
</html>