Yii2 从 dropDownList 中选择特定项目时自动输入

Yii2 Auto Input when Selecting Certain Item from dropDownList

我在名为 Kode Soal 和 Jenis Soal 的表格中输入了 2 个信息。当我从 Jenis Soal dropDownList selected 某些项目时,我想使 Kode Soal 输入自动填充。我知道我需要在这里包含一个 if 案例,但我应该在哪里包含它?这个案例不是关于从数据库中调用项目,我只想在提交之前自动填充输入。

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<div class="soal-form">

    <?php $form = ActiveForm::begin(); ?>

    <!-- THIS IS THE DROP DOWN LIST -->
    <?= $form->field($model, 'jenis_soal')->dropDownList([
        'pedagogik' => 'Kompetensi Pedagogik',
        'profesional' => 'Kompetensi Profesional',
        'kepribadian' => 'Aspek Kepribadian',
        'sosial' => 'Aspek Sosial'
    ], ['id' => 'jenisSoalId'])?>


    <!-- WHEN I SELECT E.G. Kompetensi Pedagogik, it should automatically filled "PED" -->

    <?php
    if(!empty($model->jenis_soal) && $model->jenis_soal == 'pedagogik') {
        $model->kode_soal = 'PED';
    }elseif($model->jenis_soal == 'profesional'){
        $model->kode_soal == 'PROF';
    }elseif($model->jenis_soal == 'kepribadian') {
        $model->kode_soal == 'KEP';
    }elseif($model->jenis_soal == 'sosial') {
        $model->kode_soal == 'SOS';
    }
    ?>

    <?= $form->field($model, 'kode_soal')->dropDownList([
        'PED' => 'PED', 'PROF' => 'PROF', 'KEP' => 'KEP', 'SOS' => 'SOS'
    ], ['id' => 'kodeSoalId'])?>

    <?= $form->field($model, 'soal')->textarea(['rows' => 6]) ?>


    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<script>
$("#jenisSoalId").change(function() {
     if($("#jenisSoalId").val() == 'pedagogik') {
         $("#kodeSoalId").val("PED");
     }
     if($("#jenisSoalId").val() == 'profesional') {
         $("#kodeSoalId").val("PROF");
     }
     if($("#jenisSoalId").val() == 'kepribadian') {
         $("#kodeSoalId").val("KEP");
     }
     if($("#jenisSoalId").val() == 'sosial') {
         $("#kodeSoalId").val("SOS");
     }
});
</script>

当用户 select "Kompetensi Pedagogik" 在 Jenis Soal 时,Kode Soal 输入应该自动填充文本 "PED",否则,如果 "Kompetensi Profesional" 是 selected , Kode Soal 填充 "PROF", 因此, "Kompetensi Kepribadian" 是 "KEP" 而 "Kompetensi Sosial" 是 "SOS"

试试下面的代码

<?php $form = ActiveForm::begin(); ?>

    <!-- THIS IS THE DROP DOWN LIST -->

    <?= $form->field($model, 'jenis_soal')->dropDownList([
        'pedagogik' => 'Kompetensi Pedagogik', 
        'profesional' => 'Kompetensi Profesional', 
        'kepribadian' => 'Aspek Kepribadian', 
        'sosial' => 'Aspek Sosial'
    ], ['id' => 'jenisSolaId']) ?>

    <!-- WHEN I SELECT E.G. Kompetensi Pedagogik, it should automatically filled "PED" here -->
    <?php if (!empty($model->jenis_soal) && $model->jenis_soal == 'pedagogik') : ?>
         <?php $model->kode_soal = 'PED'; ?>
    <?php endif; ?>

    <?= $form->field($model, 'kode_soal')->textInput(['id' => 'kodeSolaId', 'maxlength' => true]) ?>
    <?= $form->field($model, 'soal')->textarea(['rows' => 6]) ?>

<?php ActiveForm::end(); ?>

<script>
     $("#jenisSolaId").change(function() {
         if($("#jenisSolaId").val() == 'pedagogik') {
             $("#kodeSolaId").val("PED");
         }
     });
</script>