Yii2 Jquery onchange 下拉列表
Yii2 Jquery onchange dropdown
我正在尝试在下拉列表上使用 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>
我想要的是当我 select 来自 contact_gender 的值时,select 在 contact_title 中自动编辑一个值。
提前致谢。
p/s : 我是新手
你应该试试这个代码。
$this->registerJs("
$(function(){
$('.gender').change(function(){
getSalutationValue(this.value);
});
function getSalutationValue(value) {
if(value == 'Male'){
$('.Salutation').val('0');
}
if(value == 'Female'){
$('.Salutation').val('1');
}
if(value == 'Unspecified'){
$('.Salutation').val('2');
}
}
});
");
<?= $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') ?>
我解决了。问题是我没有声明值,我添加了 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");
}
});
我正在尝试在下拉列表上使用 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>
我想要的是当我 select 来自 contact_gender 的值时,select 在 contact_title 中自动编辑一个值。 提前致谢。
p/s : 我是新手
你应该试试这个代码。
$this->registerJs("
$(function(){
$('.gender').change(function(){
getSalutationValue(this.value);
});
function getSalutationValue(value) {
if(value == 'Male'){
$('.Salutation').val('0');
}
if(value == 'Female'){
$('.Salutation').val('1');
}
if(value == 'Unspecified'){
$('.Salutation').val('2');
}
}
});
");
<?= $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') ?>
我解决了。问题是我没有声明值,我添加了 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");
}
});