JSP+JavaScript+JavaBean打造级联菜单

index.jsp
<%@page contentType="text/html; charset=gbk" language="java" import="java.sql.*" errorPage=""%>
<jsp:useBean id="connDB" class="com.core.ConnDB" scope="page"/>
<%
  ResultSet rs_city = null;
  ResultSet rs_Town = null;
  ResultSet rs_province = null;
  String sql = "";
  String sql_T = "";
  int SelectID_C = 0;
  int SelectID_P=0;
  String strCityID1=request.getParameter("CityID");
  String strSelectID_P=request.getParameter("ProvinceID");
  if (strSelectID_P!=null &&!strSelectID_P.equals("")) {
    session.setAttribute("SelectID_P", strSelectID_P);
  }
  String sql_P = "SELECT *  FROM tb_Area  WHERE TypeID=1 or TypeID=4 or TypeID=5";
  try {
    rs_province = connDB.executeQuery(sql_P);
    if (rs_province.next()) {
      if(session.getAttribute("SelectID_P")!=null){
        SelectID_P= Integer.parseInt((String)session.getAttribute("SelectID_P"));
      }else{
        SelectID_P=rs_province.getInt("ID");
      }
      %>
      <html>
      <head>
      <title>级联菜单</title>
      <meta http-equiv="Content-Type" content="text/html; charset=gbk">
        <link href="style.css" rel="stylesheet">
        <style type="text/css">
        <!--
        body {
        margin-left: 0px;
        margin-top: 0px;
        background-image:  url(Images/bg.gif);
        }
        .style1 {color: #FFFFFF}
        .style2 {color: #a2bcc5}
        -->
        </style>
      </head>
      <script language="javascript">
      function ChangeItem_P(){
        var ProvinceID=form1.Province.value;
        window.location.href="index.jsp?ProvinceID="+ProvinceID;
      }
      </script>
      <script language="javascript">
      function ChangeItem_C(){
        var CityID=form1.city.value;
        window.location.href="index.jsp?CityID="+CityID;
      }
      </script>
      <body>
      <table width="400" height="242" border="0" cellpadding="-2" cellspacing="-2" background="Images/add.gif">
        <tr>
          <td valign="top">
            <table width="400" height="271" cellpadding="-2" cellspacing="-2">
              <tr>
                <td width="11" height="85">&nbsp;</td>
                <td width="373">&nbsp;</td>
                <td width="14">
                  <span class="style2"></span>
                </td>
              </tr>
              <tr>
                <td height="144">&nbsp;</td>
                <td valign="top">
                  <div align="center">
                    <form name="form1" method="post" action="">
                    <table width="88%" height="142" border="1" cellpadding="-2" cellspacing="-2" bordercolor="#669999" bordercolordark="#FFFFFF">
                      <tr>
                        <td width="32%" height="27" bgcolor="#809EA4">
                          <div align="center" class="style1">省级名称:</div>
                        </td>
                        <td width="68%">
                        <%
                        sql = "select* from tb_area where TypeID=2 and father=" + SelectID_P + "";
                        sql_T = "select* from tb_area where TypeID=3 and father=" + SelectID_P + "";
                        rs_city = connDB.executeQuery(sql);
                        rs_Town = connDB.executeQuery(sql_T);
                        %>
                        <div align="left">                      &nbsp;
                          <select name="Province" onChange="ChangeItem_P()">
                          <%
                          rs_province.first();
                          do {
                            %>
                            <option value="<%=rs_province.getInt("ID")%>" <%if(rs_province.getInt("ID")==SelectID_P){out.print("selected");} %>><%=rs_province.getString("Name")%> </option>
                            <%
                            } while (rs_province.next()) ;
                            %>
                            </select>
                        </div>
                        </td>
                      </tr>
                      <tr>
                        <td height="27" bgcolor="#809EA4">
                          <div align="center" class="style1">市/县名称:</div>
                        </td>
                        <td>
                          <div align="left">                      &nbsp;
                            <%if (rs_city.next()) {
                            if (strCityID1==null) {
                              SelectID_C=rs_city.getInt("ID");
                            }else{
                              SelectID_C = Integer.parseInt(request.getParameter("CityID"));
                            }
                            %>
                            <select name="city" onChange="ChangeItem_C()">
                            <%rs_city.first();
                            do {
                              %>
                              <option value="<%=rs_city.getInt("ID")%>" <%if(rs_city.getInt("ID")==SelectID_C){%>selected<%}%>><%=rs_city.getString("Name")%>                        </option>
                              <%
                              } while (rs_city.next());
                              %>
                              </select>
                          </div>
                          <%} else {                    %>
                          <select name="city" onChange="ChangeItem_C()">
                            <option value="0">---</option>
                          </select>
</div>
<%
}
%>
</td>
                      </tr>
                      <tr>
                        <td height="27" bgcolor="#809EA4">
                          <div align="center" class="style1">区/镇/乡名称:</div>
                        </td>
                        <td>
                        <%
                        if (SelectID_C != 0) {  //选择了市县级名称
                        sql_T = "select* from tb_area where father=" + SelectID_C + "";
                        System.out.println("sql_T!=0:"+sql_T);
                        rs_Town = connDB.executeQuery(sql_T);
                      }
                      %>
                      <%if (rs_Town.next()) {%>
                      <div align="left">                      &nbsp;
                        <select name="town" id="select5">
                        <%rs_Town.first();
                        do { %>
                        <option value="<%=rs_Town.getInt("ID")%>"><%=rs_Town.getString("Name")%></option>
                        <%
                        } while (rs_Town.next());
                        %>
                        </select>
                      </div>
                      <%} else {                  %>
                      <div align="left"> &nbsp;
                        <select name="town" id="select5">
                          <option value="0">---</option>
                        </select>
                      </div>
                      <%}  %>
                        </td>
                      </tr>
                      <tr>
                        <td height="33" colspan="2">
                          <div align="center">
                            <input name="Submit" type="submit" class="Style_button" value="提交">
                            <input name="Button" type="button" class="Style_button" value="关闭" onClick="javascrip:window.close()">
                          </div>
                        </td>
                      </tr>
                    </table>
                    </form>
</div>
                </td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td>          </td>
                <td>&nbsp;</td>
              </tr>
            </table>
</td>
        </tr>
      </table>
      </body>
      </html>
      <%
      }
  } catch (Exception e) {
    System.out.println("查询省级名称时出错:"+e.getMessage());
  }
%>

转载于:https://my.oschina.net/zjh92119/blog/106738

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值