Javascript jQuery价格计算不';我不能正常工作

Javascript jQuery价格计算不';我不能正常工作,javascript,jquery,html,Javascript,Jquery,Html,用于计算价格的脚本: $(window).load(function(){ $('select').on('change', function(e) { updateSubtotal(); updateDifferences(); }); function updateSubtotal() { var subtotal = 0; var start = 32; $('.math1').each(function(i, el) { sub

用于计算价格的脚本:

$(window).load(function(){

$('select').on('change', function(e) {
    updateSubtotal();
    updateDifferences();
});

function updateSubtotal() {
    var subtotal = 0;
    var start = 32;
    $('.math1').each(function(i, el) {
        subtotal += parseFloat($(this).find('option:selected').data('price')*start);
    });
    $('#display').text(subtotal);
};

function updateDifferences() {
    $('.math1').each(function(i,sel) {
        var $sel = $(sel);
        $sel.find('option').each(function(j,opt) {
            var $opt = $(opt),
                optprice = $opt.data('price'),
                selprice = $sel.find('option:selected').data('price'),
                diff = optprice * selprice,
                diffamount = Math.abs(diff) || "";
            $opt.find('.diffaddsubtr').text(diffaddsubtr).end()
        });
    });
};

});
我在每个选择选项中使用*2 writer as data price=“2”它可以正常工作,直到它必须计算多个选择的价格:然后它就会这样做

<select>
    <option data-price="2">calculate total with *2</option><!--selected-->
    <option data-price="5">calculate total with *5</option>
</select>
<select>
    <option data-price="9">calculate total with *3</option><!--selected-->
    <option data-price="3">calculate total with *9</option>
</select>

用*2计算总数
用*5计算总数
用*3计算总数
用*9计算总数

它必须这样做32(=var start)*(3*2)=192,但它会这样做:32(=var start)*(2+3)=160(=错误)我如何解决这个问题?

也许可以尝试以下方法:

var totalMult = 0;
$('.math1').each(function(i, el) {
    totalMult += parseFloat($(this).find('option:selected').data('price'));
});
subtotal = totalMult*start;

updateSubtotal

function updateSubtotal() {
    var subtotal = 32;
    $('.math1').each(function(i, el) {
        subtotal *= parseFloat($(this).find('option:selected').data('price'));
    });
    $('#display').text(subtotal);
};

在第二个选择中:您的数据属性与内部文本的值不匹配。