最新消息: 新版网站上线了!!!

Yii实现dropDownList联动操作

views中添加:
<?php
echo CHtml::dropDownList('province', '', array('province', 1=>'广东', 2=>'广西'),
    array(
        'ajax'=>array(
            'type'=>'POST',
            'url'=>Yii::app()->createUrl('site/getcity'),
            'update'=>'#city',
            'data'=>array('province'=>'js:$("#province").val()'),
        )
    )
);
?>
 
<?php
echo CHtml::dropDownList('city', '', array('city'));
?>Controller中添加:
public function actionGetcity(){
 
    if(!Yii::app()->request->isAjaxRequest)
        throw new CHttpException(404);
 
    $province = (int)$_POST['province'];
    $data = array(1=>array(1=>'广州', '深圳', '东莞', '佛山'), 2=>array(1=>'桂林', '南宁', '玉林'));
 
    if(isset($data[$province])){
        foreach ($data[$province] as $value=>$name){
            echo CHtml::tag('option', array('value'=>$value), CHtml::encode($name), true);
        }
    }else{
        echo CHtml::tag('option', array('value'=>''), 'city', true);
    }
}

转载请注明:谷谷点程序 » Yii实现dropDownList联动操作