yii html 添加下拉框,yii实现级联下拉菜单的方法

本文详细讲述了yii实现级联下拉菜单的方法,具体步骤如下:

1.模版中加入如下代码:

echo $form->dropDownList($model, 'src_type_id', OrderSrc::options(), array(

'id' => 'task-order-src-id',

));

echo $form->dropDownList($model, 'src_shop_id', array(''=>'全部'), array(

'id' => 'task-shop-id',

))

?>

在这段代码中,OrderSrc_options() 这个是先读取一个下拉菜单。调用OrderScr model中的options方法。内容如下

public static function options($hasShop = true) {

$model = new self();

if($hasShop) $model->hasShop();

$models = $model->findAll();

$array = array(''=>'全部');

foreach($models as $model) {

$array[$model->src_id] = $model->src_name;

}

return $array;

}

2.然后在模版页面中增加JS代码,实现当第一个下拉菜单变化时给第二个下拉菜单进行内容赋值。

$().ready(function(e) {

$('#task-order-src-id').change(function(e) {

refreshShops();

});

refreshShops();

function refreshShops() {

$.get('<?php echo $this->createUrl('getShops')?>', {

'srcId': $('#task-order-src-id').val()

}, function(html_content) {

$('#task-shop-id')

.html(html_content)

.find('option[value=<?php echo $model->src_shop_id?>]')

.attr('selected', 'selected');

});

}

});

在这段JS代码中,实现调取一个程序获取第二个下拉菜单的值(调用Controller中的actionGetShops方法),任何追加到第二个下拉菜单中。

Controller中的actionGetShops方法如下:

public function actionGetShops() {

$srcId = $_GET['srcId'];

$array = ThirdpartInterfaceConfig::options($srcId);

$htmlContent = "全部";

foreach($array as $k=>$v) {

$htmlContent .= "{$v}";

}

echo $htmlContent;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值