R
R
rusline2017-03-11 21:40:57
JavaScript
rusline, 2017-03-11 21:40:57

How to make list hidden when 0 value is selected?

I can’t figure out how to make it so that when the value disappears, the list should become inactive again, as active, I guessed, but if val is not equal to anything, then it becomes active. There is also an option on github if this example is not clearer https://github.com/rusline18/pergrowthnew.git
in siteController

public function actionRegion()
    {
        $region = Region::getRegion();
        foreach ($region as $regionList) {
            echo "<option value = '".$regionList->region_id."'>".$regionList->name."</option>";
        }
    }

    public function actionCity($id)
    {
       $region = City::find()->where(['region_id'=>$id])->count();
       $cityList = City::find()->where(['region_id'=>$id])->all();

       if ($region>0) {
           foreach ($cityList as $city) {
               echo "<option value = '".$city->city_id."'>".$city->name."</option>";
           }
       } echo "<option>Выберите регион</option>";
    }


in signup file
<?php $my_array = ArrayHelper::map(Region::find()->where(['country_id'=>3159])->all(),'region_id', 'name') ?>
                <?= $form->field($model, 'region')->dropDownList(
                    $my_array,
                    [
                    'prompt'=> 'Выберите регион',
                    'onchange'=>'
                        $.post("index.php?r=site/city&id='.'"+$(this).val(), function(data){
                            $("select#signupform-city").html(data);
                        });'
                    ]); ?>

                <?= $form->field($model, 'city')->dropDownList(
                    ArrayHelper::map(City::find()->all(), 'city_id', 'name'),
                    [
                    'prompt'=>'Выберите город',
                    'disabled'=>'disabled'
                    ]); ?>

And in jquery, where it is implemented that when something is selected from the list, the city index.js list becomes active
$('#signupform-region').change(function(){
  $('#signupform-city').prop('disabled',false);
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2017-03-11
@rusline

When choosing an "empty" value, 0 options is probably returned.
Do a check $("select[id=signupform-city] option").size(); If 0 leave disabled

$('#signupform-region').change(function(){
  if ($("select[id=signupform-city] option").size() != 0) {
    $('#signupform-city').prop('disabled',false);
  } else {
    $('#signupform-city').prop('disabled','disabled');
  } 
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question