Rei Tee
Rei Tee

Reputation: 125

Yii framework: dropDownList onchange not working

I have resource regarding dropDownList onchange event, but when I select my dropdownlist that is nothing happen and dint show any error message.

This is my dropdownlist in view:

<?= $form->field($model, 'pro_id')
     ->dropDownList($pro_option,         // options
     ['prompt'=>'...'] , // options
     ['onchange' => '$.post("'.Yii::$app->urlManager->createUrl(["transaction/price"]).'"+$(this).val(), function( data ) {
         $("#transactionform-r_price").html( data );
     })']);?>

<?= $form->field($model, 'r_price')->textInput(['readonly' => true]) ?>   

This is my controller(TransactionController.php) actionPrice:

public function actionPrice($id)
{
    $price = 123;
    return $price;
}

Upvotes: 0

Views: 1104

Answers (1)

Hiren Bhut
Hiren Bhut

Reputation: 1226

Please correct your syntax like this.

<?= $form->field($model, 'pro_id')->dropDownList($pro_option, [
    'prompt' => '---Select Value---',
    'onchange'=>'$.get( "'.Url::toRoute(['transaction/price']).'", { id : $(this).val() })
        .done(function(data) {
            $( "#'.Html::getInputId($model, 'r_price').'").html(data);
    });'
]) ?>

Upvotes: 1

Related Questions