view中代码

<?php
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
?>

<?php $form = ActiveForm::begin(['enableClientValidation' => false]);?>

<?= $form->field($model,'province')->dropDownList($model->getCityList(0),
    [
        'prompt'=>'--请选择省--',
        'onchange'=>'
            $(".form-group.field-member-area").hide();
            $.post("'.yii::$app->urlManager->createUrl('member/site').'?typeid=1&pid="+$(this).val(),function(data){
                $("select#member-city").html(data);
            });',
    ]) ?>

<?= $form->field($model, 'city')->dropDownList($model->getCityList($model->province),
    [
        'prompt'=>'--请选择市--',
        'onchange'=>'
            $(".form-group.field-member-area").show();
            $.post("'.yii::$app->urlManager->createUrl('member/site').'?typeid=2&pid="+$(this).val(),function(data){
                $("select#member-area").html(data);
            });',
    ]) ?>
<?= $form->field($model, 'area')->dropDownList($model->getCityList($model->city),['prompt'=>'--请选择区--',]) ?>
<?php ActiveForm::end();?>

model中添加下面的函数

    /**
     * @param $pid
     * @return array
     */
    public function getCityList($pid)
    {
        $model = City::findAll(array('pid'=>$pid));
        return ArrayHelper::map($model, 'id', 'name');
    }

控制器中添加下面的代码:

    /**
     * Function output the site that you selected.
     * @param int $pid
     * @param int $typeid
     */
    public function actionSite($pid, $typeid = 0)
    {
        $model = new Member();
        $model = $model->getCityList($pid);

        if($typeid == 1){$aa="--请选择市--";}else if($typeid == 2 && $model){$aa="--请选择区--";}

        echo Html::tag('option',$aa, ['value'=>'empty']) ;

        foreach($model as $value=>$name)
        {
            echo Html::tag('option',Html::encode($name),array('value'=>$value));
        }
    }

City数据库结构:

/*
Navicat MySQL Data Transfer

Source Server         : MySQL
Source Server Version : 50624
Source Host           : localhost:3306
Source Database       : yii2advanced

Target Server Type    : MYSQL
Target Server Version : 50624
File Encoding         : 65001

Date: 2015-07-13 14:27:12
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for yii2_city
-- ----------------------------
DROP TABLE IF EXISTS `yii2_city`;
CREATE TABLE `yii2_city` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `pid` smallint(5) unsigned NOT NULL DEFAULT '0',
  `name` varchar(120) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `parent_id` (`pid`)
) ENGINE=MyISAM AUTO_INCREMENT=3410 DEFAULT CHARSET=utf8;

Yii2.0 实现三级联动 [ 2.0 版本 ]的更多相关文章

  1. [moka同学转载]Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  2. Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  3. Yii2中省市三级联动(栏目联动)

    先从表的设计开始: 表的结构: CREATE TABLE `global_region` ( `region_id` smallint(5) unsigned NOT NULL AUTO_INCREM ...

  4. 简单的纯js三级联动

    参考这个  日尼禾尔  二级联动 写了三级联动 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. 所使用的“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”版本高于所引用的程序集“EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的版本

    错误信息:所使用的"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089&qu ...

  6. Mysql下Limit注入方法(此方法仅适用于5.0.0<mysql<5.6.6的版本)

    SQL语句类似下面这样:(此方法仅适用于5.0.0<mysql<5.6.6的版本) SELECT field FROM table WHERE id > 0 ORDER BY id ...

  7. 使用pip命令报You are using pip version 9.0.3, however version 18.0 is available pip版本过期.解决方案

    使用pip命令安装或卸载第三方库时报You are using pip version 9.0.3, however version 18.0 is available.错误,一般情况下是pip版本过 ...

  8. 报错:Unsupported major.minor version 52.0 (jar包对不同JDK版本的兼容性问题:)

    Unsupported major.minor version 52.0 这类错误是因为Java版本不一致造成的,在高版本的JDK(1.8)环境中编译JAR包,然后JAR在低版本的JVM(1.6)中运 ...

  9. 安装 tensorflow 1.1.0;以及安装其他相似版本tensorflow遇到的问题;tensorflow 1.13.2 cuda-10环境变量配置问题;Tensorflow 指定训练时如何指定使用的GPU;

    # 安装 2.7 环境conda create -n python2. python= conda activate python2. # 安装 1.1.0 gpu版本pip # 配置环境变量expo ...

随机推荐

  1. Python-正则零宽断言及命名捕获(类PHP)

    (一)零宽断言 说明:本文的例子使用python描述      首先说明一下什么是零宽断言,所谓零宽断言就是并不去真正的匹配字符串文本,而仅仅是匹配对应的位置.      正则表达式中有很多这样的断言 ...

  2. rsyslog

    http://www.rsyslog.com/ http://www.rsyslog.com/doc/v5-stable/troubleshooting/troubleshoot.html RSYSL ...

  3. jQuery.retryAjax

    Overload method for $.ajax that provides the ability to try the request over if it fails the first t ...

  4. php-mysql-excel

    <?php $fname = $_FILES['MyFile']['name']; $do = copy($_FILES['MyFile']['tmp_name'],$fname); if ($ ...

  5. 【转】keypress keydown keyup 区别

    KeyPress主要用来接收字母.数字等ANSI字符,而 KeyDown 和 KeyUP 事件过程可以处理任何不被 KeyPress 识别的击键,诸如:功能键(F1-F12).编辑键.定位键以及任何这 ...

  6. 命名空间"system.web"中不存在类型或命名空间名称security"

    在webservice中添加了一个md5加密报错: "命名空间"system.web"中不存在类型或命名空间名称security" 在引用中添加System.W ...

  7. java中 == 与 equal区别 转

    java中的数据类型,可分为两类:1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间的比较,应用双等号(==) ...

  8. 【solr】 solr 5.4.1 和tomcat 基础环境搭建

    下载省略; solr下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/ tomcat 下载安装(省略). solr5.4.1 默认在jetty ...

  9. [原]我在Windows环境下的首个Libevent测试实例

    libevent对Windows环境也有很好的支持,不过初次学习和编译libevent简单实例,总是有一些陌生感的,只有成功编译并测试了一个实例,才会有恍然大悟的感觉.下面将要讲到的一个实例是我从网上 ...

  10. VS2010提示error TRK0002: Failed to execute command解决方法

    昨天windows8自动更新Microsoft .NET Framework 3.5和4.5.1安全更新程序,今天用VS2010编译时提示如下错误信息 TRACKER : error TRK0002: ...