Yii2 - How to add onchange event in activeform

20,590

Solution 1

In your htmloptions array just do like below:

dropDownList(ArrayHelper::map(TblCategory::find()->all(), 'int_category_id', 'str_category'), array('onchange'=>'getData()','class' =>'form-control','prompt'=>'Select Room Category'))

Each key and value in htmloptions array will be converted to html attributes, for example:

'key'=>'value'

Will be shown as :

<tag key="value" /> 

Solution 2

You can call using below:

<?= $form->field($model, 'product_name')->dropDownList(ArrayHelper::map(Products::find()->all(), 'id', 'name'), 
             ['prompt'=>'-Choose a Product-',
              'onchange'=>'
                $.get( "index.php?r=suborders/listprice&id="+$(this).val(), function( data ) {
                  $( "#suborders-product_price" ).val( data );
                });
            ']);
    ?>

Hope this help you.

Share:
20,590
Kartz
Author by

Kartz

Updated on November 27, 2022

Comments

  • Kartz
    Kartz over 1 year

    This is my code :

    <?= $form->field($model, 'int_roomCatId')
              ->dropDownList(ArrayHelper::map(TblCategory::find()->all(), 'int_category_id', 'str_category'), array('class' =>'form-control','prompt'=>'Select Room Category'))
              ->label('Room Category');  ?>
    

    I want to add onchange = "getData()" event. where to add this?