HTML部分
<span style="font-family:Microsoft YaHei;font-size:12px;"><select name="id" id="column_id">
<?php foreach( (array) $columnListOne as $key => $val ):?>
<option value="<?php echo $key;?>"><?php echo $val;?></option>
<?php endforeach;?>
</select>
<select class="ml_5" name="column" id="columnlist">
<option value="">请选择分类</option>
<?php foreach( (array) $columnListTwo as $key => $val ):?>
<option value="<?php echo $key;?>"><?php echo $val;?></option>
<?php endforeach;?>
</select></span>
jQuery部分
<span style="font-family:Microsoft YaHei;font-size:12px;"><script type="text/javascript">
$(document).ready(function(){
/**
* 二级联动
*/
$("#column_id").change(function(){
var id=$(this).val();
$.post("/consult/list",{"id":id},function(list){
var data = eval( '(' + list + ')');
var html = '<option value="">请选择分类</option>';
if( data.status == 1 )
{
for ( var i=0; i<data.list.length; i++){
html += '<option value="'+ data.list[i].id +'">' + data.list[i].name + '</option>';
}
$("#columnlist").html(html);
}
else
{
$("#columnlist").html(html);
}
});
});
});
</script></span>
控制器部分
<span style="font-family:Microsoft YaHei;font-size:12px;">/**
* ajax获取二级分类
*/
public function actionList()
{
$id = Yii::app() -> request -> getParam('id');
if ( !empty( $id ) )
{
$columnList = ConsultColumn::model() -> findAll("parent_id = {$id} AND is_hidden = 0");
foreach ( (array) $columnList as $val )
{
$columnListTwo[] = array(
'id' => $val -> id,
'name' => $val -> column_name,
);
}
Util::json(array('status' => 1, 'msg' => '成功', 'list' => $columnListTwo), 1);
}
else
{
Util::json(array('status' => 0, 'msg' => '失败'), 1);
}
}</span>