当我使用带有单选按钮的 .hasVal 类时,此函数仅对我最后一次单击的值进行小计。有任何想法吗?
更新:这是 jsfiddle 链接:http: //jsfiddle.net/SUKsM/4/
本质上,我的 HTML 表单有一个带有值的字段集:
<fieldset id="breadOptions" class="hidden">
<legend>Select one</legend>
<input type="radio" name="breadType" id="breadWhite" class="hasVal" value=".5,260" /> White (260 calories)<br />
<input type="radio" name="breadType" id="breadWheat" class="hasVal" value=".5,360" /> Wheat (360 calories)<br />
</fieldset>
这是脚本:
$(function () {
var basePrice = 3;
var totalCal = 0;
var taxRate = .07;
$('#total').text(basePrice.toFixed(2));
$('#sub').text(basePrice.toFixed(2));
$('#cal').text(totalCal);
$('#tax').text((taxRate * 100).toFixed(2));
$('.hasVal').click(function () {
var price = 0;
var totalCal = 0;
var tar = event.currentTarget;
var optArr = tar.value.split(','); //cost,calorie
price += parseFloat(optArr[0]);
totalCal += parseInt(optArr[1]);
$(':checked').each(function () {
totalPrice = (price + basePrice);
});
$('#sub').text(totalPrice.toFixed(2));
$('#cal').text(totalCal);
totalTax = totalPrice + (totalPrice * taxRate);
$('#total').text(totalTax.toFixed(2));
});
});