我已经创建了动态dropDownList框。
第一个dropbox是类别,第二个dropbox是子类别。子类别dipand on category。
创建时间没有问题,但仍然选择了编辑时间类别,但未选择子类别。
所有数据都在单表中。 leval和parent_id数据到manaj类别。
===>我的表数据
| category_id | parent_id |级别| CATEGORY_NAME
| 26 | 0 | 0 |床垫
| 27 | 0 | 0 |记忆泡沫床垫
| 28 | 0 | 0 |凉床
| 103 | 26 | 1 |价值线
| 104 | 26 | 1 |公司
| 105 | 26 | 1 |枕头顶部
| 106 | 26 | 1 |单个绕线圈
======>下拉类别和子类别
<?php
$arr = array();
foreach($this->furniture_id as $furniture)
{
$id=$furniture['category_id'];
$parent_id=$furniture['parent_id'];
$cat_name=$furniture['category_name'];
if($parent_id==0)
{
$arr[$id]=$cat_name;
}
}
echo $form->labelEx($model,'category_id');
?>
<?php echo $form->dropDownList($model,'category_id', $arr ,
array('empty'=>'Please Select Category','onchange'=>'js:validate_dropdown(this.value)')); ?>
<?php echo $form->error($model,'category_id'); ?>
<?php echo $images_path = realpath(Yii::app()->basePath . '/sdmattsFurniture/get_category.php'); ?>
</div></div>
<div class="control-group" >
<div class="row">
<?php
echo $form->labelEx($model,'sub_category_id'); ?>
<?php
if($this->furniture_id != '')
{
$arr_val_sub = array();
foreach($this->furniture_id as $furniture_sub)
{
$id_sub=$furniture_sub['category_id'];
$parent_id_sub=$furniture_sub['parent_id'];
$level=$furniture_sub['level'];
$cat_name_sub=$furniture_sub['category_name'];
if($parent_id_sub > 0 && $level == 0)
{
$arr_val_sub[$id_sub]=$cat_name_sub;
}
}
echo "<div id='sub_cat'>";
echo $form->dropDownList($model,'sub_category_id', $arr_val_sub,
array('empty'=>'Please Select Category','onchange'=>'js:validate_sub_dropdown(this.value)'));
echo "</div>";?>
<?php echo $form->error($model,'sub_category_id'); ?>
<?php } ?>
</div>
</div>
=====&GT; onchange call funcation
function validate_dropdown(id) {
id=document.getElementById('SdmattsFurniture_category_id').value;
var sub_category_id=document.getElementById('SdmattsFurniture_sub_category_id').value;
if(id!='Please Select Category')
{
xmlhttpp_div_category=GetXmlHttpObject();
if (xmlhttpp_div_category==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="<?php echo Yii::app()->request->baseUrl?>/get_category.php?id="+id+"&sub_category_id="+sub_category_id;
xmlhttpp_div_category.onreadystatechange=musicChanged_div_category;
xmlhttpp_div_category.open("GET",url,true);
xmlhttpp_div_category.send(null);
}
}
感谢
答案 0 :(得分:0)
在依赖下拉列表的情况下,即dorpdonw B完全依赖于下拉A,这意味着最初下拉B将为空,当我们在下拉A中选择值时,B将按顺序填充。如果您的代码在添加期间工作正常,则我建议你在加载窗口时调用你的函数 像这样
window.load = function(){
id = id=document.getElementById('SdmattsFurniture_category_id').value;
////call your function which populate the dropdown B////
validate_dropdown(id);
}