我正在尝试在 yii2 basic 中创建一个依赖下拉列表,但它没有按预期工作。下面是创建下拉菜单的代码
<?= $form->field($model,'grp_name')->dropDownList(
ArrayHelper::map( Maingroup::find()->all(), 'id', 'name'),
[
'prompt'=>'Select your group',
'onchange'=>' $.post( "index.php?r=memberdetail/lists&id='.'"+$(this).val(), function( data ) {
$( "select#memberdetail-sub_grp" ).html( data );
});'
]); ?>
<?= $form->field($model,'sub_grp')->dropDownList(
ArrayHelper::map(NewGroup::find()->all(), 'id', 'group_num'),
[
'prompt'=>'Select your sub-group',
]); ?>
我在 memberdetail 控制器中的列表操作是
public function actionLists($id)
{
$countsubgroup = NewGroup::find()
->where(['group_name' => $id])
->count();
$subgroup = NewGroup::find()
->where(['group_name' => $id])
->all();
if ($countsubgroup > 0) {
foreach ($subgroup as $name) {
echo "<option value='" . $name->id . "'>" . $name->group_num . "</option>";
}
} else {
echo "<option> - </option>";
}
}
实际发生的是我认为这个问题,因为它不会进入 memberdetail 控制器,也不会调用公共函数 actionLists($id)