I have created a dynamic form in my yii2 app using yii2-dynamicform extension. In this form DynamicFormWidget is used to add form fields. Now I want to populate some fields with ajax on selecting one filed in this dynamic form. For a single item I can do it. I don't know to do it for added fields. Here is the form look like when generated.
<div class="panel-body">
<div class="form-group field-orderitems-0-product_code">
<label class="control-label" for="orderitems-0-product_code">Product Code</label>
<select id="orderitems-0-product_code" class="form-control" name="OrderItems[0][product_code]">
<option value="">Select</option>
<option value="#">Product</option>
</select>
<div class="help-block"></div>
</div>
<div class="row">
<div class="form-group field-orderitems-0-tax_rate">
<label class="control-label" for="orderitems-0-tax_rate">Tax Rate</label>
<input id="orderitems-0-tax_rate" class="form-control" type="text" name="OrderItems[0][tax_rate]">
<div class="help-block"></div>
</div>
</div>
</div>
Here is my jquery code:
$("#orderitems-0-product_code").change(function(){
var id = #;
$.get('get-tax-rate',{ id : id }, function(data){
var data = $.parseJSON(data)
$('#orderitems-0-tax_rate').attr('value', data.default_taxrate);
});
});
It works fine with my code for single product. What to do when adding multiple items. Form field ids will increase by 1 when dynamically generated.
Aucun commentaire:
Enregistrer un commentaire