jquery 로 2 단계 메뉴 의 인 스 턴 스 코드 생 성

3859 단어 jquery2 급메뉴
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 < jsonObj.length; i++) {
                        var $option = $('<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(<s:property value='article.subChannel.channel.channelId'/>,<s:property value='article.subChannel.subId'/>);
        });
 
페이지 코드:

<tr>
                    <th> </th>
                          <td valign="middle">
                            <s:select id='_channel' list="channelList" listKey="channelId"
                                listValue="channelName" 
                                key="article.subChannel.channel.channelId"
                                onchange="querySub(this.value)"></s:select>
                          </td>
                 </tr>
                 <tr>
                    <th> </th>
                          <td valign="middle">
                          <s:select name='article.subChannel.subId' list="{}" headerKey="" headerValue="- -"></s:select>
                          <!--<s:property value="article.subChannel.subName"/>
                          --></td>
                 </tr>
서버 쪽

 public String querySubChannelArticle(){ 
        HttpServletResponse response=getResponse();
        HttpServletRequest request=getRequest();
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter out =null;
        try {
            out = response.getWriter();

            List<Map<String,Object>> list = new LinkedList<Map<String,Object>>();
            if(channelId!=null && !channelId.equals("")){
                Channel tmp = new Channel();
                tmp.setChannelId(Integer.parseInt(channelId));
                subChannelList = subChannelDAO.listSubChannel(tmp);

                for(Object oo:subChannelList){
                    Map<String,Object> obj = new HashMap<String,Object>();
                    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;
    }  
사실 깔끔 해 요.

좋은 웹페이지 즐겨찾기