I need to perform a certain action when the value of one field changes (all the same class). However, it does not work.
$('.payment-amount').change(function () { ajax_get_supposed_money_left(); });
I'm not sure why. I am doing the same thing with an identifier instead of a class, and each tnig works fine:
$('#tripsummary-money_begin').change(function () { ajax_get_supposed_money_left(); });
The class is set correctly, because in my function I use $('.payment-amount').val() and returns the correct value. It only works when used with the change() action.
EDIT:
I use Yii2, so part of the corresponding Html looks like this:
DynamicFormWidget::begin([ 'widgetContainer' => 'dynamicform_wrapper_expenses', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 'widgetBody' => '.container-expenses', // required: css class selector 'widgetItem' => '.item-expense', // required: css class 'limit' => 99, // the maximum times, an element can be cloned (default 999) 'min' => 0, // 0 or 1 (default 1) 'insertButton' => '.add-item-expense', // css class 'deleteButton' => '.remove-item-expense', // css class 'model' => $expense_models[0], 'formId' => 'expense-create-form', 'formFields' => [ 'amount', 'category', 'comment', ], ]); ?> <div class="panel-body container-items"> <div class="panel-heading font-bold"> <button type="button" class="pull-left add-item-expense btn btn-success btn-xm"><i class="fa fa-plus"></i> <?= Yii::t('app', 'Add') ?></button> <div class="clearfix"></div> </div> <div id="payments-container" class="panel-body container-expenses"> <?php foreach ($expense_models as $index => $model): ?> <div class="item-expense"> <div> <?php <div class="row"> <div class="col-md-3"> <?= $form->field($model, "[{$index}]amount")->textInput(['class' => 'form-control payment-amount'])->label(Yii::t('app', 'Amount')) ?> </div> <div class="col-md-3"> <?= $form->field($model, "[{$index}]trip_summary_category_id")->dropDownList(ArrayHelper::map(TripSummaryCategory::find()->all(), 'id', 'name'), [ 'class' => 'form-control payment_type', ])->label(Yii::t('app', 'Category')) ?> </div> <div class="col-md-3" > <?= $form->field($model, "[{$index}]comment")->textInput()->label(Yii::t('app', 'Comment')); ?> </div> <button type="button" class="custom-remove-btn remove-item-expense btn btn-danger glyphicon glyphicon-remove"></button> </div> </div> <div class="custom-divider"></div> </div> <?php endforeach; ?> </div> <?php DynamicFormWidget::end(); ?> </div>

javascript jquery css class
Olga
source share