mercredi 30 décembre 2015

jQuery run function onchange as on/off state and not repeat itself

I have multiple checkboxes running onchange="calculatetotal()". Then I have a function for each of these checkboxes that adds/removes a row to the first empty row in the table. The problem is that since everytime a checkbox is clicked the total is calculated, when a row has been added by another checkbox, its added for a second time. How can I make each checkbox add its row only once and at the same time keep updating the total?

    calculatetotal = function () {
        var total = applecalc() + orangecalc() + pearcalc();
        alert(total);  
    }

    function applecalc() {
      var price = 0;
      var btn = $('#chkbx');
      var firstemptyrow = $('#tableid tr:empty:eq(0)');
      var removepreviousrow = $('.xxx');

      if (btn.is(':checked')) {
        price = 3;
        firstemptyrow.append('<td class="xxx">apple</td><td class="xxx">$3</td>');
      } else {
        price = 0;            
        removepreviousrow.remove();
      }
      return price;
    }




Aucun commentaire:

Enregistrer un commentaire