0 Replies - 914 Views - Last Post: 20 March 2013 - 08:20 AM

#1 javojava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-August 11

Clearing values when clicking submit again

Posted 20 March 2013 - 08:20 AM

Hello, I have a jQuery problem. I have values that are calculated and added to the respective tiers when clicking a submit button. My problem is that when you click the submit button again, it adds those calculations to the previous one. How would I go about resetting the form when you click submit again? Thanks.
$(document).ready(function() {
    $('input[type=submit]').live('click', function() {
        var numberSeats = Math.floor($('#numberSeats').val());

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

            var seatHigh = 0;
            var seatLow = 0;
            var seatBase = 0;
            var cost = 0;
            var runningTotal = 0;
            var yearTotal = 0;
            var currentYear = 0;

            $('.tier').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');

                if (currentYear != years) {
                    currentYear = years;
                    yearTotal = 0;
                }

                seats = 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;

                runningTotal =  Math.floor(costPerSeat) * Math.floor(seats) * Math.floor(years) + Math.floor(seatBase);
                $('.runningCost[year="' + currentYear + '"]').append('$' + runningTotal + '<br />');

                yearTotal += Number(Math.floor(runningTotal));
                $('.yearTotal[year="' + currentYear + '"]').append(Number(yearTotal) + '<br />');

                console.log(' Running Total ' + runningTotal + ' /' + 'Year Total ' + yearTotal);
            });
        }
    });
});



Is This A Good Question/Topic? 0
  • +

Page 1 of 1