如何解决依赖下拉列表中的错误?

时间:2019-01-23 16:31:39

标签: php yii2 dropdown

从属DropdownList可以正常工作。但是当我单击下拉列表时,我看不到选项列表

我已经阅读了所有论坛,但找不到像我这样的任何问题

index.php

$a = [
    'Qoraqalpog‘iston Respublikasi' => 'Qoraqalpog‘iston Respublikasi',
    'Andijon viloyati' =>   'Andijon viloyati',
    'Buxoro viloyati' =>  'Buxoro viloyati',
    'Jizzax viloyati' =>   'Jizzax viloyati',
    'Qashqadaryo viloyati' => 'Qashqadaryo viloyati',
    'Navoiy viloyati' =>   'Navoiy viloyati',
    'Namangan viloyati' =>   'Namangan viloyati',
    'Samarqand viloyati' =>   'Samarqand viloyati',
    'Sirdaryo viloyati' =>   'Sirdaryo viloyati',
    'Surxondaryo viloyati' =>   'Surxondaryo viloyati',
    'Toshkent viloyati' =>   'Toshkent viloyati',
    'Farg‘ona viloyati' =>   'Farg‘ona viloyati',
    'Xorazm viloyati' => 'Xorazm viloyati',
    'Toshkent shahri' => 'Toshkent shahri',
];
<?php $f = ActiveForm::begin([
                        'fieldConfig' => ['options' => ['class' => 'input-field']],
                        'enableAjaxValidation' => false,
                        'enableClientValidation'=>true,

                        'options' => [
                            'enctype' => 'multipart/form-data',
                            // 'onSubmit'=> 'return false'
                        ]
                    ])?>
                    <?= $f->field($contact,'name_of')->label(Yii::t('template','Ta\'lim muassasasi nomi yoki raqami*'))->error(false)?>
                    <?= $f->field($contact, 'city' ,['options' => ['class' => 'input-field']])->error(false)->label(Yii::t('template',"Ta'lim muassasasi joylashgan hudud*"))
                        ->dropDownList(
                            $a,
                            [
                                'prompt'=>Yii::t('template','Hududni tanlang'),
                                'onchange'=> '
                                $.post( "'.Yii::$app->urlManager->createUrl('site/regions?id=').'"+$(this).val(), function( data ) {
                                  $( "select#contactform-region" ).html( data );
                                });
                                               '
                            ]

                        );?>

                    <?= $f->field($contact,'region')->error(false)->dropDownList(
                            $empty,
                        [
                        ]);?>

actionRegions()

public function actionRegions($id){
  $posts = Regions::find()
            ->where(['idCity' => $id])
            ->all();
        $count = Regions::find()
            ->where(['idCity' => $id])
            ->count();
        $lang = Yii::$app->language;
        if ($count>0){
            foreach($posts as $post) {
                echo "<option value='".$post->id."'>".$post->{"name_".$lang}."</option>";
            }
        } else {
            echo "<option>-</option>";
        }

    }

这里是空的 https://imgur.com/a/IDso2oQ 在点击下拉菜单上没有任何反应 https://imgur.com/a/wh4ZFd9

2 个答案:

答案 0 :(得分:0)

尝试返回html标记的json字符串。

    $list ='';
    if ($count>0){
        foreach($posts as $post) {
            $list .= "<option value='".$post->id."'>".$post->{"name_".$lang}."</option>";
        }
    } else {
        $list = "<option>-</option>";
    }
    return json_encode($list);

答案 1 :(得分:0)

在我看来,您正在使用选择包装器。 我认为您应该在ajax之后更新您的选择包装插件内容。 如果您使用的是物化CSS,这是一种解决方法How to dynamically modify <select> in materialize css framework