尝试学习JQUERY / HTML,所以我正在制作购物车。我正在尝试输出小计,税金,运费和总成本到输入框。显示前2个,子总计和运费,但最后2个输入框没有输出任何内容。
HTML
<div class="form-group">
<div class="subTotal">
<label for="subtotal"><span>Sub Total</span><span>*</span><input type="number" class="input-field" name="subtotal" id="subtotal" disabled/></label>
<label for="shipping"><span>Shipping</span><span>*</span><input type="number" class="input-field" name="shipping" id="shipping" disabled/></label>
<label for="tax"><span>Tax</span><span>*</span><input type="number" class="input-field" name="tax" id="taxCost" disabled/></label>
<label for="total"><span>Total</span><span>*</span><input type="number" class="input-field" name="total" id="total" disabled/></label>
</div>
</div>
JS
function calculateSum() {
var sum = 0;
// iterate through each td based on class and add the values
$(".cost").each(function () {
var value = $(this).text();
// add only if the value is number
if (!isNaN(value) && value.length != 0) {
sum += parseFloat(value);
}
});
var subtotal = sum;
var shippingCost = (sum * 0.085);
var tax = (((sum + shipping) * 0.11));
var total = (sum + shippingCost + tax);
$("#subtotal").val(subtotal);
$("#shipping").val(shippingCost);
$("#taxCost").val(tax);
$("#total").val(total);
};
答案 0 :(得分:0)
你确定这不是一个错字吗?
var tax = (((sum + shipping) * 0.11));
不应该
var tax = (((sum + shippingCost) * 0.11));
答案 1 :(得分:0)
好吧所以我试着理解你的代码,首先我在代码中的任何地方都找不到.cost
所以从一开始就输出0
,所以我在输入中添加了一个值然后此
此代码块是您的原始代码,运费变量名称错误,可能应该是shippingCost
。
var tax = ((sum + shipping) * 0.11);