三级联动选择框

本案例的三级联动选择数据框数据来源于数据库,需要和数据库配合使用

运行效果

 HTML页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<c:if test="${empty user }">
      <%
      response.sendRedirect(request.getContextPath() + "/user?method=gotologin");
      %>
</c:if>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 <title>欢迎页面-L-admin1.0</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath }/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath }/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/css/xadmin.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/css/checkbox_radio.css">
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/js/xadmin.js"></script> 
    
    <!-- 文本框相关部件 -->
    <script type="text/javascript" src="${pageContext.request.contextPath }/wenbenkuang/js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/wenbenkuang/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/wenbenkuang/dist/bootstrap3-wysihtml5.all.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath }/wenbenkuang/dist/locales/bootstrap-wysihtml5.zh-CN.js"></script>
  </head>
  <body>
   <form method="post"  action="${pageContext.request.contextPath }/intostock?method=intostockadd" >  
    <input type="hidden" name="level" value="0">
            <div >
                <table>              
                    <tr >
                            <th style="width: 142px;">
                                <p style="margin-right: 10px; margin-top: 10px;font-size: 18px;font-family: 楷体;float: right;"><span style="color: red;" >*</span>隶属一级分类</p>
                            </th>
                            <td>
                                <select style=" margin-top: 10px;width: 230px;height: 36px;padding-left: 10px; font-size: 16px;"  id="firstclassificationid"  name="firstclassificationid"  value="" required >	
                                    <option value="">--请选择--</option>
                                </select>
                                
                            </td>
                    </tr> 
                    <tr>
                            <th style="width: 142px;">
                                <p style="margin-right: 10px;margin-top: 10px;font-size: 18px;font-family: 楷体;float: right;"><span style="color: red;">*</span>隶属二级分类</p>
                            </th>
                            <td>
                                <select style="margin-top: 10px;width: 230px;height: 36px;padding-left: 10px; font-size: 16px;"  id="twoclassificationid"  name="twoclassificationid"  value="" required >
                                    <option  value="">--请选择--</option>
                                </select>
                            </td>
                    </tr> 
                     <tr >
                            <th style="width: 142px;">
                                <p style="margin-right: 10px; margin-top: 10px;font-size: 18px;font-family: 楷体;float: right;"><span style="color: red;" >*</span>物品名称</p>
                            </th>
                            <td>
                                <select style=" margin-top: 10px;width: 230px;height: 36px;padding-left: 10px; font-size: 16px;"  id=goodsid  name="goodsid"  value="" required >	
                                    <option value="">--请选择--</option>
                                </select>
                                
                            </td>
                    </tr> 
                    <tr>
                            <th style="width: 142px;">
                                <p style="margin-right: 10px;margin-top: 10px;font-size: 18px;font-family: 楷体;float: right;"><span style="color: red;">*</span>入库类型</p>
                            </th>
                            <td>
                               <!--  <select style="margin-top: 10px;width: 230px;height: 36px;padding-left: 10px; font-size: 16px;"  id="intostocktype"  name="intostocktype"  value="" required onblur='checkName(this)' > -->
                                     <select style="margin-top: 10px;width: 230px;height: 36px;padding-left: 10px; font-size: 16px;"  id="intostocktype"  name="intostocktype"  value="" required  >
                                   <option value="">--请选择--</option>
                                   <c:forEach items="${list}" var="p">
                                     <option value="${p.intostocktypename}">${p.intostocktypename}</option>
                                  </c:forEach> 
                                   
                                   
                                </select>
                            </td>
                    </tr> 
                    <tr>
                            <th style="width: 142px;">
                                <p style="margin-right: 10px;margin-top: 10px;font-size: 18px;font-family: 楷体;float: right;"><span style="color: red;">*</span>入库数量</p>
                            </th>
                            <td>
                                <input style=" margin-top: 10px;width: 220px;height: 30px;padding-left: 10px; font-size: 16px;" type="text"  name="intonum"  name="intonum"  value="1"  readonly  required>                                                        
				               
             
                            </td>
                    </tr> 
                   
                  

                </table>

                   
            </div>
            <div >
                <button style="width: 388px;margin-left: 10px ;margin-top: 10px; font-size:30px; font-family:楷体; font-weight: 900; background-color: #0dc316;" id="sub"  type="submit" > 确定入库 </button>
            
            
            </div>            
        </form>
        
        
  </body>
  
  <script type="text/javascript">
  

	//失去焦点 发送ajax
	function checkName(obj){
		alert(obj.value)
		
		}
		
		


  




  
  //只要DOM就绪就会执行,进行异步处理    
  $(document).ready(function(){
      //声明一个全局变量,保存执行异步后获取的map对象
      var FIRSTDATE = null;

      $.ajax({
          type: "GET",
          url:"${pageContext.request.contextPath }/intostock?method=getClass",
          dataType:"json",
          success: function (firstdata) {
              //将获取的值赋值给全局变量,因为这个返回的对象要在一级菜单改变时使用,
              //所以需要扩大他的范围
              FIRSTDATE = firstdata;
              //输出打印
              console.log(firstdata);
              var grade=$("#firstclassificationid");
              for(var key in firstdata){
                  //输出打印
                  console.log("属性:" + key + "====================");
                  //根据key去取值,获得List对象
                  var list = firstdata[key];
                  //遍历这个List对象
                  for(var j = 0; j < list.length; j++){
                      var obj = list[j];
                      //一会在下面控制器代码中可以看的出来,保存map对象时,
                      //一级菜单保存的key值为0
                      //二级菜单保存为0_...
                      if(key=="0"){
                          console.log(obj);
                          //添加一级下拉菜单
                          grade.append("<option value="+obj.id+">"+obj.firstclassificationname+"</option>"); 
                      }else{
                          console.log(obj);
                      }
                  }
              }
          },
          error: function (firstdata) {
              console.log("失败")
          }
      });

      //一级菜单的改变事件
      $("#firstclassificationid").change(function(){
          var str = $("#firstclassificationid").val();
          $("#twoclassificationid").empty();
          $("#twoclassificationid").append("<option value=''>--请选择--</option>");
          if(str != "null"){
              //遍历全局变量
              for(var key in FIRSTDATE){
                  var list = FIRSTDATE[key];
                  for(var j = 0; j < list.length; j++){
                      var obj = list[j];
                      //判断并填充值
                      if(key!="0" && key=="1"+ str){
                          //输出打印
                          console.log(obj);
                          //添加二级下拉菜单
                          $("#twoclassificationid").append("<option value="+obj.id+">"+obj.twoclassificationname+"</option>"); 
                      }
                  }
              }
          }
      }); 

  //二级菜单的改变事件
  $("#twoclassificationid").change(function(){
      var str = $("#twoclassificationid").val();
      $("#goodsid").empty();
      $("#goodsid").append("<option value=''>--请选择--</option>");
      if(str != "null"){
          //遍历全局变量
          for(var key in FIRSTDATE){
              var list = FIRSTDATE[key];
              for(var j = 0; j < list.length; j++){
                  var obj = list[j];
                  //判断并填充值
                  if(key!="1" && key=="2"+ str){
                      //输出打印
                      console.log(obj);
                      //添加二级下拉菜单
                      $("#goodsid").append("<option value="+obj.id+">"+obj.goodsname+"</option>"); 
                  }
              }
          }
      }
  }); 

  });


  
  
    </script>
</html>         

后台查询数据库部分

/* 查询一级分类和二级分类以及物品名称 */
	@SuppressWarnings({ "rawtypes" })
	public void getClass(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, List> map = new HashMap<String, List>();
		FirstclassificationService firstservice = new FirstclassificationServiceImpl();
		List<Firstclassification> firstss = firstservice.findFirstList();
		map.put("0", firstss);
		for (int i = 0; i < firstss.size(); i++) {
			Firstclassification lirst = (Firstclassification) firstss.get(i);
			String id = lirst.getId();
			TwoclassificationService secondservice = new TwoclassificationServiceImpl();
			List<Twoclassification> secondss = secondservice.findSecondList(id);
			map.put("1" + id, secondss);

			for (int k = 0; k < secondss.size(); k++) {
				Twoclassification Tlist = (Twoclassification) secondss.get(k);
				String Tid = Tlist.getId();
				GoodsService goodss = new GoodsServiceImpl();
				List<Goods> goods = goodss.findGoodsList(Tid);
				map.put("2" + Tid, goods);
			}

		}
		// 输出返回
		PrintWriter out = response.getWriter();
		// 调用gson的方法将对象打为Json数据格式(字符串)
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String firstdata = gson.toJson(map);
		out.write(firstdata);// 这里括号中的参数是在页面中要获取的值,是同名的

	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我若心飞扬,天地任我逍遥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值