当前位置:首页 > 生活百科

js三级联动下拉框怎么实现(前端三级联动实现代码)

栏目:生活百科日期:2025-02-14浏览:0

数据库:

use master

go

if exists(select * from sysdatabases where name=&’stu1&′)

drop database stu1

go

create database stu1

on

(

name=&’stu1&′,

filename=&’D:\sql\stu1.mdf&’,

size=4,

filegrowth=10%

)

log on

(

name=&’stu1_log&’,

filename=&’D:\sql\stu1_log.ldf&’,

size=4,

filegrowth=10%

)

go

use stu1

go

if exists(select * from sysobjects where name=&’classes&’)

drop database classes

go

create table classes

(

id int identity primary key,

name varchar(20),

direction varchar(20)

)

insert into classes

select &‘2012271&’,&’.NET&’ union

select &‘2012272&’,&’J2EE&’ union

select &‘2012273&’,&’.NET&’ union

select &‘2012274&’,&’J2EE&’ union

select &‘2012275&’,&’J2EE&’ union

select &‘2012276&’,&’.NET&’ union

select &‘2012277&’ ,&’.NET&’

go

select * from classes

if exists(select * from sysobjects where name=&’student&’)

drop table student

go

create table student

(

id int identity primary key,

name varchar(20),

sex bit check(sex in(1,0)),

age int,

address varchar(30),

hobby varchar(30),

c_id int

)

go

insert into student

select &‘张三&’,0,20,&’内蒙赤峰&’,&’睡觉,聊天&’,1 union

select &‘李颂&’,1,19,&’湖北襄樊&’,&’逛街&’,2 union

select &‘吕两口&’,0,18,&’东北&’,&’轮滑&’,3 union

select &‘小斌&’,1,20,&’湖北十堰&’,&’做菜&’,3 union

select &‘哦哦&’,0,19,&’湖北枣阳&’,&’吃&’,4 union

select &‘康康&’,1,22,&’河南南阳&’,&’看美女&’,5 union

select &‘帅帅&’,1,18,&’湖北武汉&’,&’睡&’,5 union

select &‘忽忽&’,1,22,&’陕西西安&’,&’游戏&’,6 union

select &‘cc&’,1,22,&’湖北十堰&’,&’拼酒&’,6 union

select &‘小剑&’,0,18,&’深圳&’,&’看MM&’,7 union

select &‘勇勇&’,1,19,&’湖北十堰&’,&’吃了嘻嘻睡&’,7

go

select * from classes

select * from student,classes where student.c_id=classes.id

select * from classes

html 文件:

&<!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&>

&<title&>&</title&>

&<script src=&”jquery1.8.3/jquery-1.8.3.min.js&” type=&”text/javascript&”&>&</script&>

&<script src=&”jquery1.8.3/json2.js&” type=&”text/javascript&”&>&</script&>

&<script type=&”text/javascript&”&>

$(function () {

$.ajax({

type: &“post&”,

url: &“a.ashx&”,

success: function (msg) {

var ok = JSON.parse(msg);

$.each(ok.DataTable, function(i, j) {

var op = new Option(); //创建option对象

op.value = j.id; //option里面的value

op.text = j.name; //显示的值

//alert(op.text);

//把内容加载到select里面

document.getElementById(&“st1&”).add(op);

});

}

});

$(&“#st1&”).change(function () {

var str = $(&“#st1&”).val();

$.ajax({

type: &“post&”,

url: &“b.ashx&”,

data: { id: str },

success: function(msg) {

var kk = JSON.parse(msg);

$(&“#st2&”).empty();

var s = $(&“#tbody&”).html(&“&”);

$.each(kk.DataTable, function(o, n) {

var op1 = new Option(); //创建option对象

op1.value = n.id; //option里面的value

op1.text = n.name; //显示的值

document.getElementById(&“st2&”).add(op1);

$(&“#stu&”).attr(&“style&”, &“display:black&”);

var sex = (n.sex == &‘True&’) ? &“男&” : &“女&”;

$(&“#tbody&”).append(&“&<tr&>&<td&>&” + n.name + &“&</td&>&<td&>&” + sex + &“&</td&>&<td&>&” + n.age + &“&</td&>&<td&>&” + n.address + &“&</td&>&<td&>&” + n.hobby + &“&</td&>&<td&>&” + n.classname + &“&</td&>&</tr&>&”);

});

}

});

});

$(&“#st2&”).change(function () {

var sid = $(&“#st2&”).val();

var s = $(&“#tbody&”).html(&“&”);

$.ajax({

type: &“post&”,

url: &“c.ashx&”,

data: { id: sid },

success: function(msg) {

var ok = JSON.parse(msg);

$.each(ok.DataTable, function(l, p) {

$(&“#stu&”).attr(&“style&”, &“display:black&”);

var sex1 = (p.sex == &‘True&’) ? &“男&” : &“女&”;

$(&“#tbody&”).append(&“&<tr&>&<td&>&” + p.name + &“&</td&>&<td&>&” + sex1 + &“&</td&>&<td&>&” + p.age + &“&</td&>&<td&>&” + p.address + &“&</td&>&<td&>&” + p.hobby + &“&</td&>&<td&>&” + p.classname + &“&</td&>&</tr&>&”);

});

}

});

});

})

&</script&>

&</head&>

&<body&>

&<select id=&”st1&″&>&</select&>班级

&<select id=&”st2&″&>&</select&>学生姓名

&<br /&>

&<p id=&”stu&” style=&” display:none&”&>

&<table border=&”1&″ id=&”tab&” cellspacing=&”0&″ cellpadding=&”0&″ width=&”500&″&>

&<thead&>

&<tr&>

&<th&>学生姓名&</th&>

&<th&>学生性别&</th&>

&<th&>学生年龄&</th&>

&<th&>学生住址&</th&>

&<th&>学生爱好&</th&>

&<th&>所在班级&</th&>

&</tr&>

&</thead&>

&<tbody id=&”tbody&”&>&</tbody&>

&</table&>

&</p&>

&</body&>

&</html&>

a.ashx 文件:

public class a : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = &“text/plain&”;

DataTable dt = getTable();

string str = JSONHelper.DataTableToJsonEx(dt);

context.Response.Write(str);

}

DataTable getTable()

{

return SqlHelper.GetTable(&“select id,name from classes&”);

}

public bool IsReusable

{

get

{

return false;

}

}

}

b.ashx 文件

public class b : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = &“text/plain&”;

int id = int.Parse(context.Request[&“id&”].ToString());

DataTable dt = getTable(id);

string ss = JSONHelper.DataTableToJsonEx(dt);

context.Response.Write(ss);

}

DataTable getTable(int id)

{

StringBuilder sb = new StringBuilder();

sb.Append(&” select s.id as id,s.name as name,s.sex as sex, &“);

sb.Append(&” s.age as age,s.address as address, &“);

sb.Append(&” s.hobby as hobby,c.name as classname &“);

sb.Append(&” from student as s,classes as c &“);

string sql = string.Empty;

sql = sb.ToString();

sql += string.Format(&“where s.c_id=c.id and c.id={0}&”,id);

// string sql = string.Format(&“select * from student where c_id={0}&”,id);

return SqlHelper.GetTable(sql);

}

public bool IsReusable

{

get

{

return false;

}

}

}

c.ashx 文件

public class c : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = &“text/plain&”;

int id = int.Parse(context.Request[&“id&”].ToString());

DataTable dt = getTable(id);

string ss = JSONHelper.DataTableToJsonEx(dt);

context.Response.Write(ss);

}

DataTable getTable(int id)

{

StringBuilder sb = new StringBuilder();

sb.Append(&” select s.name as name,s.sex as sex,s.age as age, &“);

sb.Append(&” s.address as address,s.hobby as hobby, &“);

sb.Append(&” c.name as classname from student as s,classes as c &“);

string sql = string.Empty;

sql = sb.ToString();

sql += string.Format(&“where s.c_id=c.id and s.id={0}&”,id);

// string sql = string.Format(&“select s.name as name,s.sex as sex,s.age as age,s.address as address,s.hobby as hobby,c.name as classname from student as s,classes as c where s.c_id=c.id and s.id={0}&”, id);

return SqlHelper.GetTable(sql);

}

public bool IsReusable

{

get

{

return false;

}

}

}

JSONHelper.cs 辅助文件

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data;

using System.Text;

using System.Web.Script.Serialization;

/// &<summary&>

///JsonHelper 的摘要说明

/// &</summary&>

public class JSONHelper

{

private static List&<Dictionary&<string, object&>&> DataTableToList(DataTable table)

{

List&<Dictionary&<string, object&>&> list = new List&<Dictionary&<string, object&>&>();

foreach (DataRow row in table.Rows)

{

Dictionary&<string, object&> dic = new Dictionary&<string, object&>();

foreach (DataColumn column in table.Columns)

{

dic.Add(column.ColumnName,row[column.ColumnName]);

}

list.Add(dic);

}

return list;

}

public static string ObjectToJson(object obj)

{

JavaScriptSerializer jss = new JavaScriptSerializer();

return jss.Serialize(obj);

}

public static string DataTableToJson(DataTable table)

{

return ObjectToJson(DataTableToList(table));

}

/// &<summary&>

/// 通过拼字符串将DataTable转为Json

/// &</summary&>

/// &<param name=&”table&”&>&</param&>

/// &<returns&>&</returns&>

public static string DataTableToJsonEx(DataTable table)

{

string JsonName = &“DataTable&”;

StringBuilder Json = new StringBuilder(&“{\&;&” + JsonName + &“\&;:[&“);

if (table.Rows.Count &> 0)

{

foreach (DataRow row in table.Rows)

{

Json.Append(&“{&“);

foreach (DataColumn column in table.Columns)

{

Json.Append(&“\&;&” + column.ColumnName + &“\&;:\&;&” + row[column.ColumnName].ToString() + &“\&;,&”);

}

Json.Remove(Json.Length &– 1, 1);

Json.Append(&“},&”);

}

Json.Remove(Json.Length &– 1, 1);

}

Json.Append(&“]}&”);

return Json.ToString();

}

}

“js三级联动下拉框怎么实现(前端三级联动实现代码)” 的相关文章

只是付费平台有哪些(盘点10个常见的知识付费平台)

百学习是免费不抽成的知识付费平台2016年“知识付费”走入我们的视野,2017年进入疯狂发烧年,罗振宇用知识焦虑把知识付费玩的体无完肤,用户怨声载道,“观点包装...

公众号阅读量怎么计算,公众号1千阅读量收入

文章排版格式优化很多人没有把内容排版当作一个重要的事情看待,觉得排版问题不大只要用户点击进来就好了,这种思想就是错误的。试想一下当用户对你的内容还是很感兴趣的时...

十大骨瓷品牌碗(简介销量高的十大品牌骨瓷)

众所周知,陶瓷的来源是中国,但是对于骨瓷,那就很少人了解了,骨瓷最早是产于英国的。骨瓷也是现在世界上唯一公认的高档瓷种,象征着权利与地位,而这种骨瓷做成的餐具也...

ipad多大尺寸合适(2023建议买的ipad排行榜)

苹果总是带给我们违反市场规律的“惊喜”,比如9月16日发布的iPad8,起步存储仅有32GB,售价2499元,很多网友吐槽,这都2020年了,为什么苹果还要推出...

加盟天猫超市实体店要多少钱(入驻天猫超市费用解析)

天猫超市想必大家都知道,它是所有超市在网上一张名气很大的招牌。那么说到天猫超市,很多商家都在象征入驻分到其中的巨大流量。入驻天猫超市需要多少钱?天猫超市分经销,...

婴儿国产奶粉十大名牌排名(排名前三的婴儿奶粉品牌)

母乳不够,怎么办?吃奶粉容易上火?吃什么奶粉好呢?你们都买的什么奶粉啊,我不知道该怎么选&&#8230;&&#8230;等等这样的问题,一直困扰着各位妈妈。其实...

kettle javascript脚本解析对象(js脚本编写教程)

每一个成功人士的背后,必定曾经做出过勇敢而又孤独的决定。放弃不难,但坚持很酷~我们都知道Kettle是用Java语言开发,并且可以在JavaScript里面直接...

python爬虫案例讲解(简单python爬虫完整代码)

今天要分享的教程是爬取各大城市的二手房数据,抛开以前的文章不谈,下面的内容应该足够你实现这篇爬虫。以下是正文:一、确定目标今天我们的目标官网链接是链家网ok,前...

什么是品牌整合营销,浅析整合营销简单例子

文丨公关之家作者:小5引言:整合营销将企业信息通过更优的方式传递给消费者、合作伙伴,让企业学会与受众沟通,充分理解其需求,避免资源浪费的一种营销手段。单一的推广...

2020年北京中山学院怎么样(浅谈中山学院细则)

在山的那边,海的那边,有一群大学生!他们活泼又聪明,他们调皮又伶俐&&#8230;”不少大学生都会忍不住吐槽:“曾以为上了大学就是新一轮的解放,谁知道只不过是从...