Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在下拉列表yii2中设置值_Php_Yii2 - Fatal编程技术网

Php 如何在下拉列表yii2中设置值

Php 如何在下拉列表yii2中设置值,php,yii2,Php,Yii2,我有关于公民所在国家和城市的数据。这些数据使用js显示在下拉列表中。当我尝试更新它们时,下拉列表不会显示保存前的数据。如何解决这个问题。 我的代码是: <?= $form->field($organization, 'country_id')->dropDownList([],[ 'prompt' => '---', 'onchange' =&g

我有关于公民所在国家和城市的数据。这些数据使用js显示在下拉列表中。当我尝试更新它们时,下拉列表不会显示保存前的数据。如何解决这个问题。 我的代码是:

<?= $form->field($organization, 'country_id')->dropDownList([],[
                                'prompt' => '---',
                                'onchange' => '
                                    $.get("../citizeninfo/city?id='.'"+$(this).val(), function(data){
                                    $("select#training-city_id").html(data);
                                    });'
                                    ]) ?>

<?= $form->field($organization, 'city_id')->dropDownList([], ['prompt' => '---']) ?>
设置值:

<?= $form->field($model, 'sex')->dropDownList(
            ['0' => 'Male',
            '1' => 'Female']
   ) ?>

您可以这样使用:

城市模式

public static function getCityByCountry($countryId)
{
    $cities = self::find()
        ->where(['country_id' => $countryId])
        ->select(['name'])
        ->indexBy('id')
        ->orderBy(['name' => SORT_ASC])
        ->column();

    return $cities;
}
Form.php

<?= $form->field($organization, 'city_id')->dropDownList(City::getCityByCountry($organization->country_id), ['prompt' => '---']) ?>

country\u id
执行相同操作
您也可以在jquery中调用相同的函数来获取所选国家的城市