81,092
社区成员
发帖
与我相关
我的任务
分享
<script language = "JavaScript">
type2s=new Object();
<%
Connection con=OpenDB.getConnection();
String str1="select * from fgs";//先从fgs表中取分公司名称。
ResultSet rs1=ExecuteWay.exeSqlQuery(str1);
while(rs1.next()){
String str2="select distinct yddd from dfbzb1 where fgs='"+rs1.getString("name")+"' order by yddd";//从dfbzb1中取用电地点,rs1.getString("name")是从fgs表取得的分公司字段名。
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs2=stmt.executeQuery(str2);
if(rs2.next()){
rs2.last();
int rowcount=rs2.getRow();
String sz="type2s['"+rs1.getString("name")+"']=new Array(";
for(int k=1;k<=rowcount;k++){
rs2.absolute(k);
if(k<rowcount){
sz+="'"+rs2.getString("yddd")+"',";
}else {
sz+="'"+rs2.getString("yddd")+"');";
}
}
out.println(sz);
}
}
%>
function choosetype2(x,y){
var t1;
var t2;
var i,ii;
t1=x.value;
t2=y.value;
y.length=1;
if(t1=='0') return;
if(typeof(type2s[t1])=='undefined') return;
for(i=0; i<type2s[t1].length; i++)
{
ii = i+1;
y.options[ii] = new Option();
y.options[ii].text = type2s[t1][i];
y.options[ii].value = type2s[t1][i];
}
}
</script>
<TR>
<td width="20%" align="right">分 公 司:</td>
<td width="20%" align=right vAlign="center">
<select style="WIDTH: 150px" id="fgs" name="fgs" onChange="choosetype2(this,document.getElementById('yddd'))"> //onChange事件。
<option value="" selected></option>
<%
Connection con=OpenDB.getConnection();
String str="select * from fgs";//从fgs表中取分公司名称。
ResultSet rs=ExecuteWay.exeSqlQuery(str);
while(rs.next()){
out.println("<option value='"+rs.getString("name")+"'>"+ rs.getString("name")+"</option>");
}
rs.close();
%>
</select> </td>
<tr>
<td width="20%" align=right vAlign="center">用电地点: </td>
<td width="20%" align=right vAlign="center">
<!--
<Input type="text" id=yddd name="yddd" width="150px">
-->
<select name="yddd" id="yddd" style="WIDTH: 150px">
<option value=""></option>
</select>
</td>
</tr>
//定义XMLHttpRequest对象实例
var http_request = false;
/**
* 定义可复用的http请求发送函数
* method : GET 或 POST
* url : action 或 servlet
* content : request 参数组合
* responseType : text 或 xml
* callback : response返回时调用的函数
**/
function send_request(method,url,content,responseType,callback) {
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
if(responseType.toLowerCase()=="text") {
//http_request.onreadystatechange = processTextResponse;
http_request.onreadystatechange = callback;
}
else if(responseType.toLowerCase()=="xml") {
//http_request.onreadystatechange = processXMLResponse;
http_request.onreadystatechange = callback;
}
else {
window.alert("响应类别参数错误。");
return false;
}
// 确定发送请求的方式和URL以及是否异步执行下段代码
if(method.toLowerCase()=="get") {
http_request.open(method, url, true);
}
else if(method.toLowerCase()=="post") {
http_request.open(method, url, true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
else {
window.alert("http请求类别参数错误。");
return false;
}
http_request.send(content);
}
// 处理返回文本格式信息的函数
function processTextResponse() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
//alert(http_request.responseText);
alert("Text文档响应。");
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
//处理返回的XML格式文档的函数
function processXMLResponse() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
alert(http_request.responseXML);
//alert("XML文档响应。");
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/ajaxBase.js"></script>
<script type="text/javascript">
function $(objId){
return document.getElementById(objId);
}
//清除所有
function clearAll(objId){
for(var z=$(objId).options.length-1;z>-1;z--)
{
$(objId).options.remove(z);
}
$(objId).add(new Option("请选择","0"));
}
//请求此省份下的城市
function showCity(obj){
clearAll("city");
if(obj.value!=0){
send_request("post","showList","province="+obj.value,"xml",returnList);
}
}
//返回结果,加载到第二个select上
function returnList(){
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
var doc=http_request.responseXML;
var citys=doc.getElementsByTagName("city");
for(var i=0;i<citys.length;i++){
$("city").add(new Option(unescape(citys[i].getAttribute("city_name")),citys[i].getAttribute("city_id")))
}
} else { //页面不正常
alert("您所请求的页面有异常。"+http_request.status);
}
}
}
</script>
</head>
<body>
省:
<select id="province" onchange="showCity(this)">
<option value="0" >请选择</option>
<option value="1" >北京</option>
<option value="2" >上海</option>
<option value="3" >江西</option>
<option value="4" >湖北</option>
<option value="5" >湖南</option>
</select>
<br/>
市区:
<select id="city" >
<option value="0" >请选择</option>
</select>
</body>
</html>