<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="com.hanqi.dao.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>三级联动</title> <% String yij = request.getParameter("yij"); String erj = request.getParameter("erj"); int iYij = 0; if(yij != null && yij.trim().length() > 0) { iYij = Integer.parseInt(yij); //强制转换为 int 型 并赋值 } int iErj = 0; if(erj != null && erj.trim().length() > 0) { iErj = Integer.parseInt(erj); //强制转换为 int 型 并赋值 } MembersDAL md = new MembersDAL(); //一级的 ArrayList<Members> al = md.getList(0); //二级的 ArrayList<Members> al2 = null; if(iYij != 0) { al2 = md.getList(iYij); } //三级的 ArrayList<Members> al3 = null; if(iErj != 0) { al3 = md.getList(iErj); } %> <script> function yijChange() { //传参数,然后刷新页面,将二级页面刷新出来 var yiji = document.getElementById("yij"); //获取对象 window.location.href="list.jsp?yij="+yij.value; // 另一种效果完全相同 window.location="list.jsp?yij="+yij.value; } function erjChange() { var yiji = document.getElementById("yij"); //传参数,然后刷新页面,将二级页面刷新出来 var erji = document.getElementById("erj"); //获取对象 window.location.href="list.jsp?yij=" + yij.value + "&erj="+ erj.value; // 另一种效果完全相同 window.location="list.jsp?yij="+yij.value; } </script> </head> <body> <br><br> 一级地区: <select id="yij" name="yij" onchange="yijChange()"> <option value="0">未选择</option> <% if(al != null) { for(Members m : al) { out.print("<option value=‘" + m.getId()+ "‘" + (m.getId() == iYij ? "selected" : "" ) + " >" + m.getName() + "</option>"); // " + (m.getId() == iYij ? "selected" : "" ) + " //是否选中 } } %> </select> 二级地区: <select id="erj" name="erj" onchange="erjChange()"> <option value="0">未选择</option> <% if(al2 != null) { for(Members m : al2) { out.print("<option value=‘" + m.getId()+ "‘" + (m.getId() == iErj ? "selected" : "" ) + ">" + m.getName() + "</option>"); } } %> </select> 三级地区: <select id="sanj" name="sanj"> <option value="0">未选择</option> <% if(al3 != null) { for(Members m : al3) { out.print("<option value=‘" + m.getId() + "‘>" + m.getName() + "</option>"); } } %> </select> </body> </html>
原文:http://www.cnblogs.com/name-hanlin/p/5064949.html