html部分
<div class="field">
<label class="main">主类别:</label>
<select name="sh_mc_id" id="sh_mc_id" onchange="get_category();">
<option value="0">请选择</option>
<{foreach from = $main item=val}>
<option value="<{$val['mc_id']}>"><{$val['mc_name']}></option>
<{/foreach}>
</select>
<span class="error"></span>
</div>
<div class="field">
<label class="main">子类别:</label>
<span id="cate_son">
<select name="sh_sc_id" id="sh_sc_id">
<option value="">请选择</option>
</select>
</span>
<span class="error"></span>
</div>
/**
*
* ajax联动
*/
function get_category(){
var sh_mc_id = $('#sh_mc_id').val();
var empty_html = '<select name="sh_sc_id" id="sh_sc_id">';
empty_html+= '<option value="0">请选择</option>';
empty_html+= '</select>';
if(sh_mc_id>0){
var data = {
'mc_id' : sh_mc_id
};
$.ajax({
type : 'POST',
url : '/shop/getCategory',
data : data,
dataType : 'json',
success:function(json_msg){
if(json_msg.ec == 200) {
$('#cate_son').html(json_msg.em);
} else {
$('#cate_son').html(empty_html);
}
}
});
} else {
$('#cate_son').html(empty_html);
}
}
public function getCategoryAction(){
$mc_id = $this->request->getIntParam('mc_id');
$data = array('ec' => 400, 'em' => 'bad request');
if ($mc_id > 0) {
$manager = new App_Model_Category_MysqlCategorySubCategoryStorage();
$list = $manager->getListByMainID($mc_id,0,0);
$_html = $this->_deal_show_subCategory($list);
$data = array(
'ec' => 200,
'em' => $_html
);
}
$this->displayJson($data);
}
private function _deal_show_subCategory($list){
$_html = '<select name="sh_sc_id" id="sh_sc_id">';
$_html .= '<option value="0">请选择</option>';
foreach($list as $v){
$_html .= '<option value="'.$v['sc_id'].'">'.$v['sc_name'].'</option>';
}
$_html .= '</select>';
return $_html;
}