0

I'm using wbraganca dynamic form in Yii2-advanced-app. Where I want to take a custom field. But I don't understand how to handle it with onchange. Because the id's of all fields are changing dynamically as we click on 'add' button. Refer this image to clearly understand my problem

I want to change only the respected 'available qty' on changing the corresponding 'SKU'. That is if sku with id sku0 is selected, then it's available qty i.e. avlqty0 should be changed(...Sku1--->avlqty1 & so on...). But I can't get the id because it changes dynamically.

Here is my code -

<div class="col-lg-2">
    <?= $form->field($model, "[{$i}]SKU")->dropDownList(ArrayHelper::map(CompItems::find()->where(['id' => 0])->all(),'id','SKU'),['prompt' => 'Select SKU',
      'onchange' => '
           $("#avlqty").val(100);']) ?>
</div>
<div class="col-lg-3">
    <div class="form-group" style="margin-bottom: 10px;">
      <label for="avlqty">Available Qty:</label>
      <input type="text" class="form-control" id="avlqty" value="" readonly="true">
    </div>
</div>

1 Answer 1

1

Give class to the sku dropdown. Let say it is sku_class and give the id like sku_id (ski_0) and avlqty_0

$(document).on('change', '.sku_class', function() {
    ele_id = $(this).attr('id');
    ele_arr = ele_id.split("_");
    id = ele_arr.pop();  // this is your id
})
Sign up to request clarification or add additional context in comments.

6 Comments

But then it's applicable for only first 'sku_class' and not work if we try it on second sku
You need to give this class to all sku
But it's in dynamic form, we need to use it only once & it will be instantiated as we'll click on '+' button
So when you click on + and create the new element. in that new element add the class. so that all the dynamic element will also have class.
Yeah, I understand that. But this isn't the problem, the class is already added to all the elements on '+' click. What the problem I'm talking about is that, jQuery is referring to only the first position where the 'sku_class' is applied. It doesn't take it's second click/change.
|

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.