1

I have six fields which are editable in my Yii2 form widget. What i want to do is once i input integer values into editable fields calculate "Green leaf net weight" automatically based on the given input. my coding are work perfectly. But the problem is if i change already input value in the fields it'll not update the the "Green leaf net weight" read only field.

$(document).ready(function(){
  var A = $('#A');
  var B = $('#B');
  var C = $('#C');
  var D = $('#D');
  var E = $('#E');

   E.change(function(e){
     var result = ((A.val() - B.val()) * C.val())/100;
     var result2 = ((A.val() - B.val()) * D.val()) / 100;
     var final = ((A.val() - B.val()) - result - result2) - E.val(); 
     Math.round(final);
     $('#F').val(final);
   })

 });

my form widget coding (updated code)

 <?php $form = ActiveForm::begin(); ?>
 <?= $form->field($model, 'Gross_weight',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',

        ]
   ])->textInput(['id' => 'A', **'class' => 'factors'** ]) ?>

<div class="panel panel-primary" style="padding: 10px;">

    <div style="text-align: center; color: #008000;"><b>Deductions</b></div>

<?= $form->field($model, 'Bags_count',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',
        ]
   ])->textInput() ?>

<?= $form->field($model, 'Bags_weight',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',

        ]
   ])->textInput(['id' => 'B', **'class' => 'factors'** ]) ?>

<?= $form->field($model, 'Course_leaf',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',

        ]
   ])->textInput(['id' => 'C', **'class' => 'factors'**]) ?>

<?= $form->field($model, 'Water',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',

        ]
   ])->textInput(['id' => 'D', **'class' => 'factors'**]) ?>

<?= $form->field($model, 'Boiled_leaf',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',

        ]
   ])->textInput(['id' => 'E',**'class' => 'factors'**]) ?>
</div>
<?= $form->field($model, 'Greanleaf_net_weight',[
        'feedbackIcon' => [
         'default' => 'shopping-cart',
         'success' => 'ok',
         'error' => 'exclamation-sign',
        ]
   ])->textInput(['disabled' => 'disabled', 'id' => 'F']) ?>

form wiget

Thanks for everyone.

3
  • Try this: E.change(function(e){ ... }).change() Commented Mar 1, 2016 at 5:37
  • It's not working Rayon Commented Mar 1, 2016 at 5:53
  • In which case it is not working ? Commented Mar 1, 2016 at 5:54

1 Answer 1

1

Inorder to execute the calculation on the change of all the input fields you have to add a class to all the input fields and use that class as the jQuery selector for change event.

 <input id="A" class="factors" ... />
 ...
 ...

 $(".factors").change(function(e) {
   ...
   ...
 }

See Demo

Sign up to request clarification or add additional context in comments.

3 Comments

forgotten to add jquery library.
he is already using $ for selecting the input fields. So I am assuming he has included the jQuery library already.
Thanks Alex for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.