0

ユーザーの入力に応じて合計を計算するテーブルがあります。私の問題は、jqueryコードが各キーストロークを計算しており、入力をやめると数値全体を「取得」しないことです。コードは以下のとおりです。どんな助けでも大歓迎です。

$(document).ready(function() {
$('input.refreshButton').bind('click', EstimateTotal);
$('input.seatNumber').bind('keypress', EstimateTotal);
$('input.seatNumber').bind('change', EstimateTotal);

});

//$('input[type=submit]').live('click', function() {
function EstimateTotal(event) {
    var tierSelected = $(this).attr('data-year');
    var numberSeats = Math.floor($('#numberSeats_' + tierSelected).val());

    $('.alertbox_error_' + tierSelected).hide();
    if (isNaN(numberSeats) || numberSeats == 0) {
        $('.alertbox_error_' + tierSelected).show();
    } else {
        $('.alertbox_error_' + tierSelected).hide();

        var seatHigh = 0;
        var seatLow = 0;
        var seatBase = 0;
        var yearTotal = 0;
        var totalsArray = [];
        var currentYear = 0;

        $('.tier_' + tierSelected).each(function() {
            seatLow = $(this).attr('data-seat_low');
            firstSeatLow = $(this).attr('data-first_seat_low');
            seatHigh = $(this).attr('data-seat_high');
            seatBase = $(this).attr('data-base_cost');
            costPerSeat = $(this).attr('data-cost_per_seat');
            years = $(this).attr('data-year');
            seats = 0;

            if (years != currentYear) {
                if (currentYear > 0) {
                    totalsArray[currentYear] = yearTotal;
                }
                currentYear = years;
                yearTotal = 0;
            }

            if (numberSeats >= seatHigh) {
                seats = Math.floor(seatHigh - seatLow + 1);
            } else if (numberSeats >= seatLow) {
                seats = Math.floor(numberSeats - seatLow + 1);
            }

            if (seats < 0) {
                seats = 0;
            }
            yearTotal += Math.floor(costPerSeat) * Math.floor(seats) * Math.floor(years) + Math.floor(seatBase);
        });

        totalsArray[currentYear] = yearTotal;

        totalsArray.forEach(function(item, key) {
            if (item > 1000000) {
                $('.totalCost_' + tierSelected + '[data-year="' + key + '"]').append('Contact Us');
            } else {
                $('.totalCost_' + tierSelected + '[data-year="' + key + '"]').append('$' + item);
            }
        });
    }
}
4

1 に答える 1