我正在 codeigniter 中进行项目。
我动态获取两个下拉框的值,其中一个当我选择一个类别时,相应的su类别应该出现在nect drop drown中。我已经成功地使用函数检索了值,主要类别的选择需要知道如何将其重新形成
我的控制器:
public function subcategorylist($id)
{
$this->load->model('Categorymodel');
$response = array();
$response = $this->Categorymodel->getsubcategoryrdetailsbyid($id);
echo json_encode($response);
}
我的模特:
public function getsubcategoryrdetailsbyid($id)
{
$subcategoryrdetailsbyidarray = array();
$id=intval($id);
$sql = "select id, fk_parent_id, name from tbl_categories where fk_parent_id = ?";
$query = $this->db->query($sql,array($id));
if($query->num_rows())
{
$rows = $query->result();
foreach($rows as $tempsubcategorybyidarray)
{
$smallsubcatdetailsarray = array();
$smallsubcatdetailsarray['id'] = $tempsubcategorybyidarray->id;
$smallsubcatdetailsarray['fk_parent_id'] = $tempsubcategorybyidarray->fk_parent_id;
$smallsubcatdetailsarray['name'] = $tempsubcategorybyidarray->name;
array_push($subcategoryrdetailsbyidarray, $smallsubcatdetailsarray);
}
}
return $subcategoryrdetailsbyidarray;
}
我的观点:
<div class="box-body">
<div class="form-group">
<label>Sub Category</label>
<select class="form-control select product" style="width: 100%;" name="option1" id="option">
<option value="" id="subcat">Select Sub Category</option>
</select>
</div>
</div>
我的剧本:
<script>
function getsubcat(sel) {
var id = sel.value; alert (id);
var myurl="<?php echo base_url();?>index.php?/Category/subcategorylist/"+id;
$.post(myurl).done(function(data) {
alert(data); $('#subcat').html(data);
});
}
</script>
答案 0 :(得分:0)
data = [{id:1,fk_parent_id:'xyz',name:'xyz'},{id:1,fk_parent_id:'xyz',name:'xyz'}]
比你可以尝试这样
$.each(data,function(key){$('#option').append('<option value='+data[key].id+'>'+data[key].name+'</option>')})
示例小提琴:https://jsfiddle.net/hozs2uwp/
由于