我在yii框架中工作。我想创建依赖下拉框。第一个下拉框有类别,我是从数据库中获取的。我有分类下拉框的代码,因为-
<div class="row">
<?php echo $form->labelEx($model,'category'); ?>
<?php echo $form->dropdownList($model,'category', CHtml::listData(Category::model()->findAll(), 'categoryId', 'category'), array('empty'=>'Choose one')); ?>
<?php echo $form->error($model,'category'); ?>
</div>现在我想创建下一个项目的下拉框,它将只加载以上选定类别的项目。我将项目作为另一个表,该表以categoryId作为前缀关键字。那么,如何获取上述选定的categoryId,以便在同一视图窗体上加载其相关项呢?
发布于 2013-01-28 16:49:19
将jquery添加到视图中:
$('#choose-type').change(function() {
var type = $(this).val();
$.ajax({
type: "GET",
url: "<?php echo Yii::app()->request->baseUrl; ?>/your_path/chooseBrand",
data: "type="+type,
dataType: 'json',
success: function(data){
// decode data and add to your dropdown
},
});
});在你的控制器中:
public function actionChooseBrand() {
$type = (int)$_GET['type'];
$brands = Brands::model()->findAllByAttributes(array('type_id' => $type));
$data = array();
foreach($brands as $brand) {
$data[$brand->id] = $brand->name;
}
echo json_encode($data);
exit;
}我就是这么做的。:D
https://stackoverflow.com/questions/14556685
复制相似问题