javascript
页面中的函数
function querySub(id,sid/*选中的项*/){
$.post('${path}/page/article/querySubChannelArticle.action',
{'channelId':id},function(data){
$('select[name="article.subChannel.subId"] option[value!=""]').remove();
var jsonObj = data;
for ( var i = 0; i var $option = $('
'); $option.attr('value',jsonObj[i].subId); if(jsonObj[i].subId === sid){ $option.attr('selected',true); } $option.text(jsonObj[i].subName); $('select[name="article.subChannel.subId"]').append($option); } }); } $(function(){ querySub( , ); });
页面代码:
一级栏目 listValue="channelName" key="article.subChannel.channel.channelId" onchange="querySub(this.value)"> 二级栏目
服务器端
public String querySubChannelArticle(){ HttpServletResponse response=getResponse(); HttpServletRequest request=getRequest(); response.setContentType("application/json; charset=UTF-8"); PrintWriter out =null; try { out = response.getWriter(); List> list = new LinkedList>(); if(channelId!=null && !channelId.equals("")){ Channel tmp = new Channel(); tmp.setChannelId(Integer.parseInt(channelId)); subChannelList = subChannelDAO.listSubChannel(tmp); for(Object oo:subChannelList){ Map obj = new HashMap(); SubChannel c = (SubChannel)oo; obj.put("subId", c.getSubId()); obj.put("subName", c.getSubName()); list.add(obj); } } String outStr = JsonUtil.toJSONStr(list); out.println(outStr); }catch(Exception e){ e.printStackTrace(); } out.flush(); out.close(); return null; }
其实蛮简洁的