index.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta content="" name="author">
<title>无限级联动菜单[AJAX版]</title>
<style type="text/css">
body, td
{
font-family: 宋体;
font-size: 12px;
line-height:180%;
}
</style>
<script language="JavaScript">
function BuildSel(str,sel)
{
sel.options.length=0;
var arrstr = new Array();
arrstr = str.split("|");
sel.options.add(new Option( "-----请选择-----",""));
if(str.length>0)
{
for(var i=0;i<arrstr.length;i++)
{
var subarrstr=new Array;
subarrstr=arrstr[i].split(",");
if(subarrstr[1]!=" "&&subarrstr[1]!=null)
sel.options.add(new Option(subarrstr[1],subarrstr[0]));
}
sel.options[0].selected=true;
}
}
/
function GetResult(typeStr,dropid)
{
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
alert(typeStr);
alert(dropid);
var linkurl="dropData.jsp?typeStr="+typeStr+"&numValue="+dropid;
http_request.open("GET",linkurl,false);
http_request.send(null);
var returntxt=unescape(http_request.responseText.replace(" "));
//alert(dropid.length);
//alert(returntxt);
if(returntxt.length>0)
{
//document.all.ajax.innerHTML="服务器返回结果:<font color='red'>"+returntxt+"</font> [Powered by AJAX]"
if(typeStr=="wx")
BuildSel(returntxt,document.getElementById('sel2'));
if(typeStr=="khdb")
BuildSel(returntxt,document.getElementById('sel3'));
}
else
{document.all.ajax.innerHTML=""}
}
</script>
<form name="form1" method="post" action="">
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="F1F1F1">
<td height="24" colspan="2" align="center">琥珀无限级联动菜单-AJAX版 [睿翔网络科技修改]</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="12%" height="24" align="center">行区:</td>
<td><select name="sel1" id="sel1" onChange="GetResult('wx',this.value)">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt1 id=txt1 size=10></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="24" align="center">外县:</td>
<td><select name="sel2" id="sel2" onChange="GetResult('khdb',this.value)"">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt2 id=txt2 size=10></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="24" align="center">客户代表:</td>
<td><select name="sel3" id="sel3">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt3 id=txt3 size=10></td>
</tr>
<tr bgcolor="F1F1F1">
<td height="24" colspan="2" align="center" id="ajax"> </td>
</tr>
</table>
<script language="JavaScript">
///
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
var linkurl="dropData.jsp?typeStr=xq";
http_request.open("GET",linkurl,false);
http_request.send(null);
var returntxt=unescape(http_request.responseText)
BuildSel(returntxt,document.getElementById('sel1'));
</script>
</form>
</body>
</html>
//*********************************************************
dropData.jsp(连接数据库的JSP页面)
<%@ page contentType="text/html;charset=gb2312"%>
<% response.addHeader("Cache-Control","no-cache"); %>
<%@ page import="java.sql.*"%>
<%
/数据库连接
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@130.36.30.209:1521:crm";
String user="crm";
String password="crmrnd";
String sqlStr="11";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
/数据库连接
//得到index.jsp的值
String typeStr = request.getParameter("typeStr");
String numValue = request.getParameter("numValue");
//得到index.jsp的值
//根据不同的option的值采用不同的sql语句
if(typeStr.equalsIgnoreCase("xq"))
sqlStr = "select * from crm_tbm_xq";
else if(typeStr.equalsIgnoreCase("wx"))
sqlStr = "select * from crm_tbm_wx where xq = '"+numValue+"'";
else if(typeStr.equalsIgnoreCase("khdb"))
sqlStr = "select * from crm_user_name Where wx = '"+numValue+"'";
//根据不同的option的值采用不同的sql语句
ResultSet rs=stmt.executeQuery(sqlStr);
while(rs.next()){
%>
<%=rs.getString(1)%>,<%=rs.getString(3)%>|
<%}%>
<%rs.close();
stmt.close();
conn.close();
%>
以上代码全部是别人的,我只是加点文字说明,方便和我一样的人,看还不会,问也不明白的
下面是我自己 更改点部分代码 使用我的javabean 操作数据库,别的地方都是一样的
index.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta content="" name="author">
<title>无限级联动菜单[AJAX版]</title>
<style type="text/css">
body, td
{
font-family: 宋体;
font-size: 12px;
line-height:180%;
}
</style>
<script language="JavaScript">
function BuildSel(str,sel)
{
//alert(str);
sel.options.length=0;
var arrstr = new Array();
arrstr = str.split("|");
sel.options.add(new Option( "-----请选择-----",""));
if(str.length>0)
{
for(var i=0;i<arrstr.length;i++)
{
var subarrstr=new Array;
subarrstr=arrstr[i].split(",");
if(subarrstr[1]!=" "&&subarrstr[1]!=null)
sel.options.add(new Option(subarrstr[1],subarrstr[0]));
}
sel.options[0].selected=true;
}
}
/
function GetResult(typeStr,dropid)
{
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
alert(dropid);
var linkurl="dropData.jsp?typeStr="+typeStr+"&numValue="+dropid;
http_request.open("GET",linkurl,false);
http_request.send(null);
var returntxt=unescape(http_request.responseText.replace(" "));
//alert(dropid.length);
//alert(returntxt);
if(returntxt.length>0)
{
document.all.ajax.innerHTML="服务器返回结果:<font color='red'>"+returntxt+"</font> [Powered by AJAX]"
if(typeStr=="wx")
BuildSel(returntxt,document.getElementById('sel2'));
if(typeStr=="khdb")
BuildSel(returntxt,document.getElementById('sel3'));
}
else
{document.all.ajax.innerHTML=""}
}
</script>
<form name="form1" method="post" action="">
<table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="F1F1F1">
<td height="24" colspan="2" align="center">琥珀无限级联动菜单-AJAX版 [睿翔网络科技修改]</td>
</tr>
<tr bgcolor="#FFFFFF">
<td width="12%" height="24" align="center">行区:</td>
<td><select name="sel1" id="sel1" onChange="GetResult('wx',this.value)">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt1 id=txt1 size=10></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="24" align="center">外县:</td>
<td><select name="sel2" id="sel2" onChange="GetResult('khdb',this.value)"">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt2 id=txt2 size=10></td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="24" align="center">客户代表:</td>
<td><select name="sel3" id="sel3">
<option value="" selected>-----请选择-----</option>
</select><input type=text name=txt3 id=txt3 size=10></td>
</tr>
<tr bgcolor="F1F1F1">
<td height="24" colspan="2" align="center" id="ajax"> </td>
</tr>
</table>
<script language="JavaScript">
///
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
var linkurl="dropData.jsp?typeStr=xq";
http_request.open("GET",linkurl,false);
http_request.send(null);
var returntxt=unescape(http_request.responseText)
BuildSel(returntxt,document.getElementById('sel1'));
</script>
</form>
</body>
</html>
dropData.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<jsp:useBean id="query" scope="session" class="weblistbean.QueryBean" />
<%@ page language="java" contentType="text/html;charset=gb2312"
import="java.sql.*"
import="java.io.*"
import="java.util.*"
import="javax.sql.*"
import="javax.naming.*"
import="java.text.*"
import="java.lang.*"
%>
<% response.addHeader("Cache-Control","no-cache"); %>
<%
String sqlStr="11";
String typeStr = request.getParameter("typeStr");
String numValue = request.getParameter("numValue");
if(typeStr.equalsIgnoreCase("xq"))
sqlStr = "select SITE_ID,SITE_NAME from T_DIC_SUBSITE ";
else if(typeStr.equalsIgnoreCase("wx"))
sqlStr="SELECT T_DIC_CHANNELSET.INFECTANT_ID,T_DIC_INFECTANT.INFECTANT_NAME,T_DIC_CHANNELSET.SITE_ID FROM T_DIC_CHANNELSET INNER JOIN T_DIC_INFECTANT ON T_DIC_CHANNELSET.INFECTANT_ID = T_DIC_INFECTANT.INFECTANT_ID WHERE T_DIC_CHANNELSET.SITE_ID ='"+numValue+"'";
else if(typeStr.equalsIgnoreCase("khdb"))
sqlStr = "select * from T_DIC_INFECTANT Where INFECTANT_ID = '"+numValue+"'";
ResultSet rs=query.getResult(sqlStr);
while(rs.next())
{
%>
<%=rs.getString(1)%>,<%=rs.getString(2)%>|
<%
}
%>