|
发表于 2011-11-4 10:49:11
|
显示全部楼层
Re:求级联下拉列表的例子
我写了个是asp+ajax的,还没有用jsp+ajax写呢.抱歉了.
function CallBackObject()
{
this.XmlHttp = this.GetHttpObject();
}
CallBackObject.prototype.GetHttpObject = function()
{
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
CallBackObject.prototype.DoCallBack = function(URL)
{
if( this.XmlHttp )
{
if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
{
var oThis = this;
this.XmlHttp.open('OST', URL);
this.XmlHttp.onreadystatechange = function(){
oThis.ReadyStateChange(); };
this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
this.XmlHttp.send(null);
}
}
}
CallBackObject.prototype.AbortCallBack = function()
{
if( this.XmlHttp )
this.XmlHttp.abort();
}
CallBackObject.prototype.OnLoading = function()
{
// Loading
}
CallBackObject.prototype.OnLoaded = function()
{
// Loaded
}
CallBackObject.prototype.OnInteractive = function()
{
// Interactive
}
CallBackObject.prototype.OnComplete = function
(responseText, responseXml)
{
// Complete
}
CallBackObject.prototype.OnAbort = function()
{
// Abort
}
CallBackObject.prototype.OnError = function(status, statusText)
{
// Error
}
CallBackObject.prototype.ReadyStateChange = function()
{
if( this.XmlHttp.readyState == 1 )
{
this.OnLoading();
}
else if( this.XmlHttp.readyState == 2 )
{
this.OnLoaded();
}
else if( this.XmlHttp.readyState == 3 )
{
this.OnInteractive();
}
else if( this.XmlHttp.readyState == 4 )
{
if( this.XmlHttp.status == 0 )
this.OnAbort();
else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
else
this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);
}
}
/=====================================/
上面的是ajax 中使用的CallBakcObject.js 文件
这个调用.
var cbo = new CallBackObject();
cbo.OnComplete = Cbo_Complete_forPre;
cbo.onError = Cbo_Error;
cbo.DoCallBack(url);
/===================/
asp+ajax实现无限制级级连菜单(一)
主页面.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" src="js/CallBackObject.js"></script>
</head>
<script language=jscript><br>
var idCount=0;<br>
var commonStr = "<option value='0'>请选择</option>";<br>
var base_name="newclass_";//菜单的id名字的基本字符串。<br>
var nextId;//当前菜单的菜单的名字。<br>
var currentId;<br>
var currentIdNumber;//当前处理菜单的id数字部分。<br>
var doNext;//是当前的id还是往下处理。<br>
var h_cando_id="h_cando";<br>
var h_lastid_id="h_lastid"<br>
//生成子菜单的名字<br>
function createNextId(fn)<br>
{<br>
var c_id = fn.id;<br>
var len = base_name.length;<br>
var n_id = c_id.substr(len);<br>
n_id++;<br>
currentIdNumber = n_id;<br>
nextId = base_name+n_id;<br>
<br>
return base_name+n_id; <br>
}<br>
//Cur_Done当前处理数据。<br>
function Cur_Done(fn)<br>
{<br>
var cu_idnum = fn.id.substr(base_name.length);<br>
if (cu_idnum==idCount)<br>
return true;<br>
return false;<br>
}<br>
//处理选择动作<br>
function createRequest(orderId,fn)<br>
{<br>
orderId=0;<br>
createNextId(fn);<br>
currentId = fn.value;<br>
var flag = Cur_Done(fn);<br>
//判断当前文件夹是否是最后一个。<br>
if (!flag)<br>
{<br>
orderId=1;<br>
}<br>
var id = escape(document.getElementById(fn.id).value);<br>
var cbo = new CallBackObject();<br>
switch(orderId)<br>
{<br>
case 0:<br>
if (id==0)<br>
return false;<br>
cbo.OnComplete = Cbo_Complete;<br>
cbo.onError = Cbo_Error;<br>
cbo.DoCallBack("testAjax.asp?id=" + id); <br>
break;<br>
case 1:<br>
id = escape(document.getElementById(fn.id).value);<br>
<br>
if (id==0)<br>
return false;<br>
cbo.OnComplete = Cbo_Complete_forPre;<br>
cbo.onError = Cbo_Error;<br>
cbo.DoCallBack("testAjax.asp?id=" + id); <br>
break;<br>
} <br>
}<br>
function Cbo_Complete(responseText, responseXML)<br>
{<br>
idCount++;<br>
//alert("nextId is "+nextId);<br>
<br>
var next_b = document.getElementById("testli");<br>
var nextclass = new Array();<br>
nextclass = responseText.split(';');<br>
var returnStr="";<br>
var returnLine = new Array();<br>
returnStr ="<select name='"nextId"' id='"nextId"' onChange='javascript:createRequest(0,this)'>";<br>
returnStr+=commonStr;<br>
for (i=0;i<nextclass.length-1;i++)<br>
{<br>
returnLine = nextclass.split(':');<br>
returnStr +="<option value='"returnLine[0]"'>";<br>
returnStr +=returnLine[1];<br>
returnStr +="</option>";<br>
}<br>
returnStr +="</select>";<br>
next_b.innerHTML =" " returnStr;<br>
<br>
do_post_flag(nextclass.length,currentId);<br>
//alert(responseText);
}<br>
function Cbo_Complete_forPre(responseText, responseXML)
{
var cur_id_number = currentIdNumber;
var next_b = document.getElementById(nextId);
//处理当前菜单的子菜单级数
for (i=idCount;i>=cur_id_number;i--)
{
var do_name = base_name+i;
var do_id = document.getElementById(do_name);
//从最后一个处理子菜单的内容,清空后赋值
for (j=do_id.length-1;j>=0;j--)
{
do_id.options[j]=null;
do_id.length=0;
}
var do_id_option = document.createElement("option");
do_id_option.text = "请选择";
do_id_option.value = "0";
do_id.options.add(do_id_option);
}
for (i=next_b.length-1;i>=0;i--)
{
next_b.options = null;
next_b.length=0;
}
var nextclass = new Array();
nextclass = responseText.split(';');
var returnStr="";
var returnLine = new Array();
var op = document.createElement("option");
op.text = "请选择";
op.value ="0";
next_b.options.add(op);
for (i=0;i<nextclass.length-1;i++)
{
returnLine = nextclass.split(':');
var option = document.createElement("option");
option.text = returnLine[1];
option.value = returnLine[0];
next_b.options.add(option);
}
do_post_flag(nextclass.length,currentId);
}<br>
function do_post_flag(cur_len,cur_id)
{
var v_candoid = document.getElementById(h_cando_id);
var v_lastid = document.getElementById(h_lastid_id);
v_lastid.value = currentId;
if (cur_len==1)
v_candoid.value =false;
else
v_candoid.value=true;
}<br>
function Cbo_Error(status, statusText, responseText)
{
alert(responseText);
}<br>
</script>
<body>
<%
set conn = Server.CreateObject("adodb.connection")
connstring="provider=sqloledb;server=localhost;uid=sa;pwd=sa;Database=ajaxTest;"
conn.open connstring
set rs = Server.CreateObject("adodb.recordset")
rs.open "select id,infront,idclassflag,newclass from newclass_2001 where infront=0" ,conn,1,1
response.Write("<select name='newclass_0' id='newclass_0' onChange='javascript:createRequest(0,this)'>")
Response.Write("<option value='0'>请选择</option>")
do while not rs.eof
Response.Write("<option value='"&rs("idclassflag")&"'>"&rs("newclass")&"</option>")
rs.movenext
loop
response.Write("</select>")
%>
<label id="testli"></label>
下一级是否有可选项<input type="text" name="h_cando" id="h_cando">
最后选项的名字<input type="text" name="h_lastid" id="h_lastid">
</body>
</html>
/===================/
asp+ajax实现无限制级级连菜单(二)testAjax.asp
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
set conn = Server.CreateObject("adodb.connection")
connstring="provider=sqloledb;server=localhost;uid=sa;pwd=sa;Database=ajaxTest;"
conn.open connstring
set rs = Server.CreateObject("adodb.recordset")
rs.open "select * from newclass_2001 where infront='"&request("id")&"'" ,conn,1,1
do while not rs.eof
Response.Write(rs("idclassflag")&":"&rs("newclass")&";")
rs.movenext
loop
%>
生成数据库脚本
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[newclass_2001]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[newclass_2001]
GO
CREATE TABLE [dbo].[newclass_2001] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[infront] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[idclassflag] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[newclass] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
infront 字段为父文件夹的id;
idclassflag为自身的id; |
|