最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Yii2 Jquery onchange dropdown - Stack Overflow

programmeradmin0浏览0评论

I'm trying to use onclick on a dropdownlist.

This is my view :

<?= $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') ?>

and this is my function :

<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>

What I want is when I select a value from contact_gender,a value is selected automatically in contact_title. Thanks in advance.

p/s : I'm a newbie.

I'm trying to use onclick on a dropdownlist.

This is my view :

<?= $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') ?>

and this is my function :

<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>

What I want is when I select a value from contact_gender,a value is selected automatically in contact_title. Thanks in advance.

p/s : I'm a newbie.

Share Improve this question edited Apr 20, 2016 at 8:43 Brad Pott asked Apr 20, 2016 at 8:20 Brad PottBrad Pott 911 gold badge1 silver badge9 bronze badges 1
  • 2 If what you've provided is the code you're using the function names are different. onchange is getSalutationValue while the function is getSolutationValue. – Alex Commented Apr 20, 2016 at 9:11
Add a ment  | 

2 Answers 2

Reset to default 3

I solved it. The problems are I didn't declare the value and I added on.change twice. Here's the correct code. Thanks for the response.

<?= $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");
 }
});

You should try this code.

$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') ?>
发布评论

评论列表(0)

  1. 暂无评论