dimanche 20 septembre 2015

Uncheck Checkbox if Checkbox and Text Input Values Aren't Equal

I have a table inside a form that I'm using to make payments on invoices. On each table row there is a checkbox and a text input. When the user clicks the checkbox, it populates the text input with the invoice balance.

In some instances, a user will click the checkbox and populate the text input as noted above. However, the user may then decide that they do not want to pay the full balance on that invoice. So, they can change the value of the text input to any amount less than the balance. When a user does this, I want to uncheck the checkbox.

P.S. Note that the icheck plugins is being used here, hence the ifChecked event usage.

jQuery Code:

  $(function () {
      var payFull = $('input[type="checkbox"].payfull');
      var payNow = $('input[type="text"].paynow');
      var payAmt = $('#amounttopay');

      // Recalc Function
      function reCalc() {
          var sum = 0;
          $(payNow).each(function () {
              sum += Number($(this).val());
          });
          if (!isNaN(sum) && sum.length !== 0) {
              $(payAmt).html('$' + sum.toFixed(2));
          } else {
              $(payAmt).html('$0.00');
          }
      }
      // When Pay in Full Checkbox is Checked fill in Pay This Time Field with Invoice Amount Due Value
      $(payFull).on('ifChecked', function (event) {
          var val = $(this).val().replace('$', '');
          var price = $(this).closest('tr').find('input[type="text"]').val(val);
          $(price);
          $(reCalc);
      });

      // If Pay This Time changes recalculate total
      var payFullVal = $('input[type="checkbox"].payfull').val().replace('$', '');
      var payNowVal = $('input[type="text"].paynow').val();
      $(this).keyup(function () {
          $(reCalc);
          if (payFullVal !== payNowVal) {
              payNow.closest('tr').find('input').iCheck('uncheck');
          } else {}
      });
  });

Explanation of what happens: When I click the checkbox and modify the text input value all checkboxes are unchecked. I want just the checkbox on the same table row as the modified text input to be unchecked.

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire