javascript - Calculate value auto into form field using JS -


i have 6 fields editable in yii2 form widget. want once input integer values editable fields calculate "green leaf net weight" automatically based on given input. coding work perfectly. problem if change input value in fields it'll not update the "green leaf net weight" read field.

$(document).ready(function(){   var = $('#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 everyone.

inorder execute calculation on change of input fields have add class input fields , use class jquery selector change event.

 <input id="a" class="factors" ... />  ...  ...   $(".factors").change(function(e) {    ...    ...  } 

see demo


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -