私はドロップダウンリストでonclickを使用しようとしています。Yii2 Jquery onchangeドロップダウン
これが私の見解です:
<?= $form->field($model, "contact_gender")->dropDownList(ArrayHelper::map($genders, 'value', 'description'),['class'=>'form-control','prompt'=>'Please Select','onchange'=>'getSalutationValue','required'=>true])->label('Gender') ?>
<?= $form->field($model, "contact_title")->dropDownList(ArrayHelper::map($salutations, 'value', 'description'),['class'=>'form-control Salutation','prompt'=>'Please Select','required'=>true])->label('Salutation') ?>
と、これは私の関数である:
<script>
function getSolutationValue() {
var value = (this.value);
if(value == 'Male'){
$('.Salutation').val('0');
}
if(value == 'Female'){
$('.Salutation').val('1');
}
if(value == 'Unspecified'){
$('.Salutation').val('2');
}
}
</script>
私が欲しいものは、私がcontact_genderから値を選択すると、値はcontact_titleに自動的に選択されています。 ありがとうございます。
p/s:私は初心者です。
使用しているコードが機能名と異なる場合は、 'onchange'は' getSalutationValue'で、関数は 'getSolutationValue'です。 – Alex