我在表单中输入了2个输入,分别是Kode Soal和Jenis Soal。当我从Jenis Soal dropDownList中选择某些项目时,我想使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>
当用户在Jenis Soal中选择“ Kompetensi Pedagogik”时,Kode Soal输入应自动填充文本“ PED”,否则,如果选择“ Kompetensi Profesional”,则Kode Soal中将填充“ PROF”,因此,“ Kompetensi Kepribadian” “是“ KEP”,“ Kompetensi Sosial”是“ SOS”
答案 0 :(得分:0)
尝试下面的代码
<?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>