在view端:
<?php echo $form->labelEx($model, 'test'); ?>
<?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);
}
}
转载于:https://blog.51cto.com/8023java/1748803