Learning
Learning

Reputation: 20001

Calculate Value of form fields & update Total

I have a form where i need to auto calculate value for checked row.

http://codepen.io/anon/pen/dOBaNN?editors=1010

I am calculating the value for the text field on keyup function but need to do following

  1. Keep All text fields disabled
  2. Second i need to enable only text field related to the checkbox checked
  3. Third Calculate sum for only Checked text field

Right now i am able to calculate value for all checkbox not sure how to map checked checbox to related input and calculate values accordingly.

tried few thing but it keep breaking

Upvotes: 0

Views: 475

Answers (1)

pixelarbeit
pixelarbeit

Reputation: 494

1) Too disable your text fields set the disabled property to true.

$(".auto-sum").each(function () {
     $(this).prop('disabled', true);
});

2) Enable inputs with checkboxes

$('input[type=checkbox]').on('change', function() {
    var id = $(this).val();
    var active = $(this).prop('checked');
    $('.auto-sum[name=Amount' + id + ']').attr('disabled', !active);
    calculateSum();
});

3) Skip disabled inputs when calculating

$(".auto-sum").each(function () {
     if ($(this).prop('disabled')) return;
     [...]
});

I updated your codepen: http://codepen.io/anon/pen/VmJgxM?editors=1011

Upvotes: 2

Related Questions