這是從給定的鏈接我的示例程序(http://www.yiiframework.com/wiki/24/)
<!-- ===================== My First Dropdown =========== -->
<?php
echo CHtml::dropDownList('country_id', '', array(1 => 'USA', 2 => 'France', 3 => 'Japan'), array(
'ajax' => array(
'type' => 'POST', //My method type
'url' => CController::createUrl('site/CallCities'), //This is my request/ajax URL
array('coountry_id'=>'js:this.value'), //I'm passing the selected dropdonw value.
'dataType' => 'JSON',
'success'=>'js:function(data)' //The functionaliy after success
. '{'
. ' var html="";'
. ' $.each(data,function(i,obj)'
. ' {'
. ' html+="<option value=obj.id>"+obj.name+"</option>"'
. ' });'
. ' $("#city_id").html(html);'
. '}'
)));
?>
<!-- ===================== My Second Dropdown =========== -->
<?php
echo CHtml::dropDownList('city_id', '', array());
?>
而且,我的控制方法是
public function actionCallCities()
{
$cityId=$_POST['coountry_id'];
$criteria=new CDbCriteria();
$criteria->select=array('id,name');
$criteria->condition='city_id='.$cityId;
$criteria->order='name';
$cityAry= MyCity::model()->findAll($criteria);
$ary=array();
foreach($cityAry as $i=>$obj)
{
$ary[$i]['id']=$obj->id;
$ary[$i]['name']=$obj->name;
}
echo json_encode($ary);
}
我希望,這個例子程序會爲你
工作,請發佈HTML以及。 –
+1,可能clientChage參數並未在視圖中設置 –
另外,爲什麼不使用'CHtml :: dropDownList()'? – casraf