The content of this article is about how Yii implements the linkage event (code) of dropDownList. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Due to functional requirements, linkage needs to be used, hereby record and share
<div> <?php $form = ActiveForm::begin(); ?> = $form->field($model, 'cid')->dropDownList( \common\models\service\common\Category::getCateOptions(), [ 'prompt' => '请选择单位', 'onchange'=>' $.post("/carousel/main/cate?id='.'"+$(this).val(),function(data){ $("select#main-aid").html(data); });', ] )->hint('父级,必选') ?> = $form->field($model, 'aid')->dropDownList( \common\models\service\common\Category::getCateOptions(), ['prompt' => '请选择子级']) ?> <div> = Html::submitButton('Save', ['class' => 'btn btn-success']) ?> </div> <?php ActiveForm::end(); ?> </div>
ublic function actionCate($id) { $departments = Category::find() ->where(['parent_id' => $id]) ->asArray() ->all(); if(!$departments){ echo "<option>" . "</option>"; } foreach ($departments as $department) { echo "<option>" . $department['name'] . "</option>"; } }
Related recommendations:
Yii2 uses dropdownlist to realize the regional three-level linkage function
Realization of mutual linkage between dropdownlists (showing and hiding)_javascript skills
The above is the detailed content of How to implement the linkage event of dropDownList in Yii (code). For more information, please follow other related articles on the PHP Chinese website!