在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); } }
本文出自 “Java大白的战地” 博客,请务必保留此出处http://8023java.blog.51cto.com/10117207/1748803
PHP 的YII框架中CActiveForm dropDownList联动效果
原文地址:http://8023java.blog.51cto.com/10117207/1748803