我正在尝试在下拉列表上使用onclick。
这是我的看法:
<?= $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中自动选择一个值。提前致谢。
ps:我是新手。
我解决了 问题是我没有声明值,而是两次添加on.change。这是正确的代码。感谢您的回复。
<?= $form->field($model, "contact_gender")->dropDownList(ArrayHelper::map($genders, 'value', 'description'),['class'=>'form-control gender','prompt'=>'Please Select','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') ?>
$this->registerJs('
$(".gender").change(function(){
var value = this.value;
if(value == "MALE"){
$(".Salutation").val("0");
}
if(value == "FEMALE"){
$(".Salutation").val("4");
}
if(value == "UNSPECIFIED"){
$(".Salutation").val("23");
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句