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