【问题标题】:How to solve my code dependent dropdown without extentions如何在没有扩展的情况下解决我的代码相关下拉列表
【发布时间】:2016-06-05 15:44:38
【问题描述】:

当我选择Id Province时,我无法选择Id City。如何解决?

控制器代码

此代码供查看

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及重现它所需的最短代码in问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example请不要发布代码图片。
  • 欢迎来到 SO。请访问help center 并使用tour 了解如何询问以及询问什么。提示:发布代码,而不是代码图片
  • 分享你的代码而不是图片 :)

标签: php html json yii2


【解决方案1】:

我之前通过关注tutorial 完成了此操作。下面是我的代码。希望对您有所帮助。

考虑中

<div class='col-lg-4'>
            <?= $form->field($modelsBahagian, 'bahagian_id')->dropDownList(
                ArrayHelper::map(TblBahagian::find()->all(), 'bahagian_id', 'bahagian_nama'),
            [
                'prompt'=>'Pilih Bahagian',
                'onchange'=>'
                    $.post("index.php?r=tbl-moderator/lists&id='.'"+$(this).val(), function( data ) {
                        $( "select#tblbhgnmod-unit_id" ).html( data );
                    });'
            ]); ?>
        </div>
        <div class='col-lg-4'>
            <?= $form->field($modelsBahagian, 'unit_id')->dropDownList(
                ArrayHelper::map(TblUnit::find()->all(),'unit_id','unit_nama'),
                ['prompt'=>'Sila Pilih unit']
            ) ?>
        </div>

在控制器中

public function actionLists($id)
{
    $countUnits = TblUnit::find()
        ->where(['bahagian_id' => $id])
        ->count();

    $units = TblUnit::find()
        ->where(['bahagian_id' => $id])
        ->all();

    if($countUnits > 0) {

        foreach($units as $unit){
            echo "<option value='".$unit->unit_id."'>".$unit->unit_nama."</option>";
        }
    }
    else{
        echo "<option> - </option>";
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多