源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 539|回复: 14

[ASP编程] 一个强健 实用的asp+ajax二级联动菜单(有演示和附源程序打包下载)

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2007-7-19 00:00:00 | 显示全部楼层 |阅读模式
一个强健 实用的ajax二级联动菜单(有演示和附源程序打包下载)

前些天在搞后台的时候要用到二级联动的菜单,到网上去找了半天也没找到满意的,不是这错就是那错,在选择的时候有时候不能返回.真是郁闷.
     后来就看到有人用ajax写了无限级分类(牛,呵呵,本人看不懂.).就想到我那个后台不也可以用ajax试试,虽然比用javascript的慢点,但我这个实用.强健..不会出错,也不会选择某个大类后再返回无选择状态小类为空的状态.呵呵.
文件目录:如图

主要代码如下:
index.asp
复制代码 代码如下:
<%@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" c />
<title>Ajax的二级联动by啊峰</title>
<script language="javascript" src="js.js"></script>
</head>
<body>
<h2><a href="http://yeahdown.com/">Ajax的二级联动by啊峰</a></h2>
<!--#include file="iconn.asp"-->
<% 
Set afeng = Conn.Execute("select bigclassid,bigclassname from bigclass")
%>
<form id="form1" name="form1" method="post" action="">
  <div id="bigclass" style="float:left">
  <select name="select" >
     <option value="0">选择一级分类</option>
      <%If Not afeng.Eof then
        Do While Not afeng.Eof
            bigclassid= afeng("bigclassid")
            bigclassname = afeng("bigclassname")%>
            <option value="<%=bigclassid%>"><%=bigclassname%></option>
        <%afeng.Movenext
        Loop
    End If
    afeng.Close
    Set afeng = Nothing
    Conn.Close
    Set Conn = Nothing%>
  </select>
  </div>
  <div id="subclass"  style="float:left"><select name="select2">
    <option value="0">选择二级分类</option>
  </select>
  </div>
</form>
</body>
</html>

getsubcategory.asp文件:
复制代码 代码如下:
<%
Response.Charset="GB2312" '设置字符集 原默认为utf-8
bigclassid=safe(request.QueryString("bigclassid")) '非法字符过滤
If bigclassid<>"" Then
        Set Re=New RegExp
        Re.IgnoreCase=true
        Re.Global=false
        Re.Pattern = "^[0-9]{1,3}$"
        If Not Re.Test(bigclassid) Then 
            Response.Write "非法参数"
            Response.End
        End If%>
<!--#include file="iconn.asp"-->
    <%On error Resume Next
    set p = conn.execute("select * from smallclass where bigclassid="&bigclassid&"")
    If Err Then
        Err.Clear
        Response.Write "查询出错"
        Response.End 
    End If
    If Not p.Eof Then
        html = "<select name='smallclassid'>"&vbnewline
        Do While Not p.Eof
        html = html&"<option value='"&p("smallclassid")&"'>"&p("smallclassname")&"</option>"&vbnewline
        p.Movenext
        Loop
        html = html&"</select>"
    Else
        html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>"
    End If
    p.Close
    Set p = Nothing
    Conn.Close
    Set Conn = Nothing
    Response.write html
    html = ""
End If
%>

iconn.asp文件: 
复制代码 代码如下:
<%
Set conn=server.CreateObject("adodb.connection")
db = "$#yeahdown.com@$$$@@%%.asp"
C & Server.MapPath(db)
On Error Resume Next
conn.open ConnStr
If Err Then
    Err.Clear
    Response.Write "<div id=""error"">连接数据库出错</div>"
    Response.End
End If

Function safe(Str)
If Isnull(Str) Then
     safe = ""
     Exit Function 
End If
Str = Replace(Str,Chr(0),"", 1, -1, 1)
Str = Replace(Str, """", """, 1, -1, 1)
Str = Replace(Str,"<","<", 1, -1, 1)
Str = Replace(Str,">",">", 1, -1, 1) 
Str = Replace(Str, "script", "script", 1, -1, 0)
Str = Replace(Str, "SCRIPT", "SCRIPT", 1, -1, 0)
Str = Replace(Str, "Script", "Script", 1, -1, 0)
Str = Replace(Str, "script", "Script", 1, -1, 1)
Str = Replace(Str, "object", "object", 1, -1, 0)
Str = Replace(Str, "OBJECT", "OBJECT", 1, -1, 0)
Str = Replace(Str, "Object", "Object", 1, -1, 0)
Str = Replace(Str, "object", "Object", 1, -1, 1)
Str = Replace(Str, "applet", "applet", 1, -1, 0)
Str = Replace(Str, "APPLET", "APPLET", 1, -1, 0)
Str = Replace(Str, "Applet", "Applet", 1, -1, 0)
Str = Replace(Str, "applet", "Applet", 1, -1, 1)
Str = Replace(Str, "[", "[")
Str = Replace(Str, "]", "]")
Str = Replace(Str, """", "", 1, -1, 1)
Str = Replace(Str, "=", "=", 1, -1, 1)
Str = Replace(Str, "'", "''", 1, -1, 1)
Str = Replace(Str, "select", "select", 1, -1, 1)
Str = Replace(Str, "execute", "execute", 1, -1, 1)
Str = Replace(Str, "exec", "exec", 1, -1, 1)
Str = Replace(Str, "join", "join", 1, -1, 1)
Str = Replace(Str, "union", "union", 1, -1, 1)
Str = Replace(Str, "where", "where", 1, -1, 1)
Str = Replace(Str, "insert", "insert", 1, -1, 1)
Str = Replace(Str, "delete", "delete", 1, -1, 1)
Str = Replace(Str, "update", "update", 1, -1, 1)
Str = Replace(Str, "like", "like", 1, -1, 1)
Str = Replace(Str, "drop", "drop", 1, -1, 1)
Str = Replace(Str, "create", "create", 1, -1, 1)
Str = Replace(Str, "rename", "rename", 1, -1, 1)
Str = Replace(Str, "count", "count", 1, -1, 1)
Str = Replace(Str, "chr", "chr", 1, -1, 1)
Str = Replace(Str, "mid", "mid", 1, -1, 1)
Str = Replace(Str, "truncate", "truncate", 1, -1, 1)
Str = Replace(Str, "nchar", "nchar", 1, -1, 1)
Str = Replace(Str, "char", "char", 1, -1, 1)
Str = Replace(Str, "alter", "alter", 1, -1, 1)
Str = Replace(Str, "cast", "cast", 1, -1, 1)
Str = Replace(Str, "exists", "exists", 1, -1, 1)
Str = Replace(Str,Chr(13),"<br>", 1, -1, 1)
safe = Replace(Str,"'","''", 1, -1, 1)
End Function
%>

最后一个js.js 
复制代码 代码如下:
function createxmlhttp()
{
    xmlhttpobj = false;
    try{//创建对象,一个一个的试,哎,要是能统一标准都好。。
        xmlhttpobj = new XMLHttpRequest;
    }catch(e){
        try{
            xmlhttpobj=new ActiveXObject("MSXML2.XMLHTTP");
        }catch(e2){
            try{
                xmlhttpobj=new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e3){
                xmlhttpobj = false;
            }
        }
    }
    return xmlhttpobj; 
}
function getsubcategory(bigclassid){
    if(bigclassid==0){
        document.getElementById("subclass").innerHTML="<select name='smallclassid'><option value='0' selected>选择二级分类</option></select>";
        return;
    };
    var xmlhttpobj = createxmlhttp();
    if(xmlhttpobj){//如果创建对象xmlhttpobj成功
        xmlhttpobj.open('get',"getsubcategory.asp?bigclassid="+bigclassid+"&number="+Math.random(),true);//get方法 加个随机数。
        xmlhttpobj.send(null);
        xmlhttpobj.onreadystatechange=function(){//客户端监控函数
            if(xmlhttpobj.readystate==4){//服务器处理请求完成
                if(xmlhttpobj.status==200){
                    //alert('ok');
                    var html = xmlhttpobj.responseText;//获得返回值
                    document.getElementById("subclass").innerHTML=html;
                }else{
                    document.getElementById("subclass").innerHTML="对不起,您请求的页面有问题...";
                }
            }else{
                document.getElementById("subclass").innerHTML="加载中,请梢候...";//服务器处理中
            }
        }    
    }
}

打包文件下载
回复

使用道具 举报

27

主题

1万

回帖

331

积分

中级会员

Rank: 3Rank: 3

积分
331
发表于 2022-8-18 17:45:57 | 显示全部楼层
飞飞飞飞飞飞飞飞飞飞飞飞飞
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-9-4 14:25:03 | 显示全部楼层
论坛有你更精彩!
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

182

积分

注册会员

Rank: 2

积分
182
发表于 2022-11-24 14:03:58 | 显示全部楼层
谢谢您的分享!
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-1-13 07:49:05 | 显示全部楼层
加快速度很快就撒谎
回复 支持 反对

使用道具 举报

8

主题

1万

回帖

52

积分

注册会员

Rank: 2

积分
52
发表于 2023-2-22 21:30:55 | 显示全部楼层
儿飞飞微风DVD谁vdsvd
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

73

积分

注册会员

Rank: 2

积分
73
发表于 2023-3-12 01:26:50 | 显示全部楼层
看看怎么样再说
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-4-14 14:26:59 | 显示全部楼层
很不错的源码论坛
回复 支持 反对

使用道具 举报

11

主题

1万

回帖

300

积分

中级会员

Rank: 3Rank: 3

积分
300
发表于 2023-9-3 13:39:40 | 显示全部楼层
管灌灌灌灌灌灌灌灌灌灌
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

307

积分

中级会员

Rank: 3Rank: 3

积分
307
发表于 2023-10-11 00:04:21 | 显示全部楼层
给爸爸爸爸爸爸爸爸爸爸八佰伴八佰伴
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-21 08:06 , Processed in 0.077183 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表