简体   繁体   English

计算2/5项后的价格表+折扣

[英]Calculating price list + discount after 2/5 items

I am working on a interactive price list for a friend of mine. 我正在为我的一个朋友制作交互式价格表。 She want a price list, where the customer can check her prices. 她想要一个价目表,客户可以在其中查看她的价格。 If the customer clicks on multiple options the price is automatically calculated. 如果客户单击多个选项,则会自动计算价格。 There are two price lists. 有两个价目表。 One for Woman and one for Man. 一个给女人,一个给男人。
That is working so far. 到目前为止,这是有效的。

The problem which I've got is: When I change between Woman and Man, the price is still in the "total" field. 我遇到的问题是:当我在女人和男人之间切换时,价格仍然在“总计”字段中。
But the biggest problem for me is, that when the customer selects 2-4 options he get a discount of 10% and 5 or more options he get 20% discount. 但是对我来说最大的问题是,当客户选择2-4个选项时,他将获得10%的折扣,而5个或更多选项时,他将获得20%的折扣。

Here you can see my codepen 在这里你可以看到我的codepen

$(document).ready(function() {
    $('ul.tabs li').click(function() {
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#" + tab_id).addClass('current');

    })
})

$(function() {
    $('input').click(function() {

        var total = 0;
        $('input:checked').each(function(index, item) {
            total += parseFloat(item.value);
        });
        $('.total').text(total);
    });
});

$('#head_checkbox').on('change', function() {
    if ($(this).is(':checked')) {
        $('.person').attr('checked', true);
    } else {
        $('.person').attr('checked', false);
    }
});

$('.person').click(function() {
    var total_length = $('.person').length;
    var total_checked_length = $('.person:checked').length;

    if (total_length == total_checked_length) {
        $('#head_checkbox').attr('checked', true);
    } else {
        $('#head_checkbox').attr('checked', false);
    }
});

var checked = false;
$('.all').click(function() {
    e = $(this).attr('name');
    checked = !checked;
    $('input[class=' + e).prop('checked', checked);
});

1) Change the selector to only match inputs in the current tab. 1)将选择器更改为仅匹配当前选项卡中的输入。

2) Count the number of checked options, and apply a discount to the total when you're done. 2)计算选中选项的数量,完成后对总数量应用折扣。

$(function() {
    $('input').click(function() {

        var total = 0;
        var option_count = 0;
        $('.tab-content.current input:checked').each(function(index, item) {
            total += parseFloat(item.value);
            option_count++;
        });
        // Apply multiple product discount
        if (option_count >= 5) {
            total *= .80;
        } else if (option_count >= 2) {
            total *= .90;
        }
        $('.total').text(total);
    });
});

Fixes for "total" filed on different tabs: 修复了在不同选项卡上“总计”的问题:

$(document).ready(function() {
        tab_1_total = 0;
        tab_2_total = 0;
        tab_id = "";
        $('ul.tabs li').click(function() {
                tab_id = $(this).attr('data-tab');

                $('ul.tabs li').removeClass('current');
                $('.tab-content').removeClass('current');

                $(this).addClass('current');
                $("#" + tab_id).addClass('current');

                if( tab_id == "tab-1") {
                    $('.total').text(tab_1_total);
                }

                else if( tab_id == "tab-2") {
                    $('.total').text(tab_2_total);
                }
        })
})

$(function() {
        $('input').click(function() {

                var total = 0;
                var option_count = 0;   

                $('.tab-content.current input:checked').each(function(index, item) {
                        total += parseFloat(item.value);
                        option_count++;
                });
                // Apply multiple product discount
                if (option_count >= 5) {
                        total *= .80;
                } else if (option_count >= 2) {
                        total *= .90;
                }

                total = total.toFixed(2);
                $('.total').text(total);

                if( tab_id == "tab-1") {
                    tab_1_total = total;
                }

                else if( tab_id == "tab-2") {
                    tab_2_total = total;                    
                }

        });
});

$('#head_checkbox').on('change', function() {
        if ($(this).is(':checked')) {
                $('.person').attr('checked', true);
        } else {
                $('.person').attr('checked', false);
        }
});

$('.person').click(function() {
        var total_length = $('.person').length;
        var total_checked_length = $('.person:checked').length;

        if (total_length == total_checked_length) {
                $('#head_checkbox').attr('checked', true);
        } else {
                $('#head_checkbox').attr('checked', false);
        }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM