Search code examples
yii2onchangemaskedinput

How to use 'onchange' in Yii2 MaskedInput widget


I have an Yii2 app with advanced template. There is 2 fields on my form. 1st: MaskedInput and 2nd textInput with readonly attribute. So, I wanted to fill 2nd textInput automatically right after I have filled MaskedInput. For this I tried to use onchange. But I got following error:

Unknown Property – yii\base\UnknownPropertyException
Setting unknown property: yii\widgets\MaskedInput::onchange

Here is my code:

<?php $form = ActiveForm::begin(); ?>
<div class="row">
    <div class="col-xs-3">
        <?= Html::label("Ser num")?>
        <?= MaskedInput::widget(['name'=>'serNum',
            'mask'=>'AA 9999999',
            'onchange'=>'
                $.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
                $("select#ser-sernum").html(data);
        });
            '
        ])?>
    </div>
    <div class="col-xs-3">
        <?= $form->field($model, 'sernum')->textInput(['readonly'=>true]) ?>
    </div>
    <div class="col-xs-6">
        <b id="actstatus"></b>
    </div>
</div>

Solution

  • Try with options property:

    <?= yii\widgets\MaskedInput::widget(['name'=>'serNum',
            'mask'=>'AA 9999999',
            'options' => [
              'onchange'=>'
                  $.post("index.php?r=act/actid&serNum='.'"+$(this).val(),function(data){
                  $("select#ser-sernum").html(data);
               });'
             ]
    ])?>