javascript - 使用selects和jQuery计算价格无法正常工作

标签 javascript jquery html shop

我有这个剧本:

$(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/

相关文章:

javascript - 函数在两个不同元素上执行不同

javascript - AngularJS 错误加载 Controller

jQuery 可排序在 IE 中不起作用

javascript - 使用 Javascript/Jquery 禁用桌面 Web 浏览器中的页面缩放

java - 从JFrame应用程序到Applet

javascript - js 中的 php json_encode 返回不需要的结果

javascript - Angular Directive(指令)的属性

javascript - Highcharts : plot line click event for multiple chart

javascript - 阻止鼠标滚轮滚动,但不阻止滚动条事件。 JavaScript

html - 无限期地在 bootstrap 中循环 divs?