我有一个使用 javascript 汇总其值的表单,它可以正常工作,直到您输入一个包含美分的货币值,因此 2.99 美元的值将仅显示 2.00。这是我的 jsFiddle http://jsfiddle.net/vegas/JwFCB/
function multiplyRow(row) {
var this_qty = row + "_value1"
var this_price = row + "_value2"
var this_extend = "extend" + row;
document.getElementById(this_extend).value = (parseInt(document.getElementById(this_qty).value) * parseInt(document.getElementById(this_price).value)) || 0;
summate();
}
function rateRow(row) {
var this_payrate = "payrate" + row;
var this_hours = "hours" + row;
var this_subtot = "subtotal" + row;
document.getElementById(this_subtot).value = (parseInt(document.getElementById(this_payrate).value) * parseInt(document.getElementById(this_hours).value)) || 0;
getlabor();
}
function summate() {
var materialcost = 0
for (var i = 1; i <= 5; i++) {
var id = "extend" + i;
var thisVal = parseInt(document.getElementById(id).value, 10) || 0;
materialcost += thisVal;
}
document.getElementById("materialcost").value = materialcost || 0;
tax();
grandTotal();
}
function getlabor() {
var labortotal = 0
var hourCounter = 0
for (var i = 1; i <= 5; i++) {
var id = "subtotal" + i;
var idHours = "hours" + i;
var thisVal = parseInt(document.getElementById(id).value, 10) || 0;
var thisValHours = parseInt(document.getElementById(idHours).value, 10) || 0;
labortotal += thisVal;
hourCounter += thisValHours;
}
document.getElementById("labortotal").value = labortotal || 0;
document.getElementById("totalhours").value = hourCounter || 0;
grandTotal();
}
function tax() {
var materialcost = document.getElementById('materialcost').value || 0;
var shipping = document.getElementById('shipping').value || 0;
var salestax = (Math.round(((materialcost / 100) * 8.1) * 100) / 100) || 0;
var materialtotal = ((materialcost * 1) + (salestax * 1) + (shipping * 1)) || 0;
document.getElementById('materialcost').value = materialcost;
document.getElementById('salestax').value = salestax;
document.getElementById('shipping').value = shipping;
document.getElementById('materialtotal').value = materialtotal;
grandTotal();
}
function grandTotal() {
var labor = parseInt(document.getElementById("labortotal").value, 10) || 0;
var material = parseFloat(document.getElementById("materialtotal").value, 10) || 0;
document.getElementById('grandtotal').value = (labor + material).toFixed(2);
}