我是Yii的新手,想要创建依赖的下拉列表,我使用的是cActive表单... 能够填充第一个下拉列表,但第二个不是自动填充的
我想要做的是当用户在下拉列表中选择一个州时,第二个下拉列表应该仅自动填充该州的区域。
MT表
state_details
STATEID Statename的
state_district
ID STATEID statedistrict
视图
<div class="row">
<?php echo $form->labelEx($model,'stateDetails'); ?>
<?php
echo $form->dropDownList($model,'statename',CHtml::listData(StateDetails::model()->findAll(), 'stateid', 'statename'),
array(
'prompt'=>'Select State',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('loadStateDistrict'), //or $this->createUrl('loadstatedistrict') if '$this' extends CController
'update'=>'#statedistrict', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('stateid'=>'js:this.value'),
))
);
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state_district'); ?>
<?php
echo CHtml::dropDownList('statedistrict','', array(), array('prompt'=>'Select District'));
?>
</div>
controller
public function actionLoadStateDistrict()
{
$data=state_district::model()->findAll('stateid=:stateid',
array(':stateid'=>(int) $_POST['stateid']));
$data=CHtml::listData($data,'id','statedistrict');
foreach($data as $value=>$statedistrict)
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($statedistrict),true);
}
答案 0 :(得分:0)
您的success
来电更新了元素#statedistrict_stateid
,但您的相关下拉列表的ID为statedistrict
,因为您直接使用了CHtml
。您可以使用htmlOptions