我有这个剧本:
$(window).load(function(){
$('.math1').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);
});
$('#price').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()
});
});
};
});
只要你只使用一个选择框,它就工作得很好,但是只要你使用多个选择框,它就会总结出所有的值,我怎样才能更改它,使它乘以这些值
当你和一个6人的房间一起去时,价格应该是192美元,但会是160美元
这是我的问题:http://development.sakesalverda.nl/Hotel/reserveren/(价格将更新,但不正确)
最佳答案
你可以试试这个:
subtotal += parseFloat( $('.math1').val() ) * start;
而不是
$('.math1').each(function(i, el) {
subtotal += parseFloat($(this).find('option:selected').data('price')*start);
});
$('.math1').val()=>它将返回选定的值。。
关于javascript - 使用selects和jQuery计算价格无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23542189/