欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-27 09:17:40
数据库:use mastergoif exists(select * from sysdatabases where name=’stu1′)drop database stu...

数据库:

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 />

<div 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>

</div>

</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();

}

}