36.二级菜单联动

//Channel.java
package com.imooc.ajax;

public class Channel {
    private String code;
    private String name;
    public Channel(){
        
    }
    public Channel(String code, String name) {
        super();
        this.code = code;
        this.name = name;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}
//ChannelServlet.java
package com.imooc.ajax;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class ChannelServlet
 */
@WebServlet("/channel")
public class ChannelServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChannelServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String level = request.getParameter("level");
         String parent = request.getParameter("parent");
         List chlist = new ArrayList();
         if(level.equals("1")) {
             chlist.add(new Channel("ai","前沿/区块链/人工智能"));
             chlist.add(new Channel("web","前端/小程序/Js"));
         }else if(level.equals("2")) {
             if(parent.equals("ai")) {
                 chlist.add(new Channel("micro","微服务"));
                 chlist.add(new Channel("blockchain","区块链"));
                 chlist.add(new Channel("other","..."));
             }else if(parent.equals("web")) {
                 chlist.add(new Channel("html","HTML"));
                 chlist.add(new Channel("css","CSS"));
                 chlist.add(new Channel("other","..."));
             }
         }
         String json = JSON.toJSONString(chlist);
         response.setContentType("text/html;charset=utf-8");
         response.getWriter().println(json);
    }

}
//cascade_menu.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-3.4.1.js"></script>
<script type="text/javascript">
    $(function(){ //页面就绪函数
        $.ajax({
            "url":"/Ajax/channel",
            "data":{"level":"1"},
            "type":"get",
            "dataType":"json",
            "success":function(json){
                console.log(json);
                for(var i=0;i<json.length;i++){
                    var ch = json[i];
                    $("#lv1").append("<option value='"+ch.code+"'> " + ch.name + "</option>");
                }
            }
        })
    })
    
    
            
    $(function(){
        $("#lv1").on("change",function(){
            var parent = $(this).val();
            console.log(parent);
            $.ajax({
                "url" : "/Ajax/channel",
                "data" : {"level":"2","parent":parent},
                "dataType" : "json",
                "type" : "get" ,
                "success" : function(json){
                    console.log(json);
                    //移除所有lv2下的原始option选项
                    $("#lv2>option").remove();
                    for(var i=0;i<json.length;i++){
                        var ch = json[i];
                        $("#lv2").append("<option value='"+ch.code+"'> " + ch.name + "</option>");
                    }
                }
            })
        })
    })
</script>
</head>
<body>
    <select id="lv1" style="width:200px;height:30px">
        <option selected="selected">请选择</option>
    </select>
    <select id="lv2" style="width:200px;height:30px">   
    </select>
</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容