Yii2 - 如何从DepDrop中获取所选值

时间:2016-01-14 01:07:31

标签: yii2 selected

我使用这个模块http://demos.krajee.com/widget-details/depdrop 这是我的代码:

_form:

<?= $form->field($model, 'categories')->dropDownList($catList, ['id'=>'cat_name', 'prompt'=>'Select category']);?>

<?= $form->field($model, 'subcategories')->widget(DepDrop::classname(), [
            'options'=>['id'=>'sub_name'], 
            'pluginOptions'=>[
            'depends'=>['cat_name'],
            'placeholder'=>'Select subcategory',
            'url'=>  Url::to(['ad/subcat'])
            ]
            ]);  ?>

和控制器:

public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];

if ($parents != null) {
$id = $parents[0];
$out=\backend\models\Subcategories:find()->where(['cat_name'=>$id])->select(['id','sub_name AS name'])->asArray()->all();
echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}

它工作正常,但我想显示或隐藏DIV,具体取决于所选的子类别。

这是我的代码: 这是我的代码:

 <?php $this->registerJs(' $(\'select[id="sub_name"]\').change(function(){

    if ($(\'option:selected\').val() == 3)

       document.getElementById("table").style.display = "block";
    else 
       document.getElementById("table").style.display = "none";

    });', View::POS_READY);
    ?>

但它不起作用,它从类别下拉列表中获取值。 如何从所选子类别中获取值?

0 个答案:

没有答案