怎样用js 在jsp页面实现二级联动
展开全部
参考下列代码:
<html>
< head>
< title>多级关联菜单</title>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< head>
< script language="JavaScript">
< !--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",["安徽","北京","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江","重庆"]);
dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
//-->
< /script>
< script language = JavaScript>
< !--
var s=["s1","s2","s3"];
var opt0 = ["-省-","-市-"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
< /script>
< /head>
< body onload="setup()">
< form name="frm">
< select id="s1"><option>省</option></select>
< select id="s2"><option>市</option></select>
< /form>
< /body>
< /html>
<html>
< head>
< title>多级关联菜单</title>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312">
< head>
< script language="JavaScript">
< !--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}
function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}
var dsy = new Dsy();
dsy.add("0",["安徽","北京","福建","甘肃","广东","广西","贵州","海南","河北","河南","黑龙江","湖北","湖南","吉林","江苏","江西","辽宁","内蒙古","宁夏","青海","山东","山西","陕西","上海","四川","天津","西藏","新疆","云南","浙江","重庆"]);
dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
//-->
< /script>
< script language = JavaScript>
< !--
var s=["s1","s2","s3"];
var opt0 = ["-省-","-市-"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
< /script>
< /head>
< body onload="setup()">
< form name="frm">
< select id="s1"><option>省</option></select>
< select id="s2"><option>市</option></select>
< /form>
< /body>
< /html>
展开全部
/*显示、验证省市*/
var citys=new Array();
citys["山东省"]=["济南市","青岛市","潍坊市","烟台市","威海市","滨州市","其他"];
citys["江苏省"]=["南京市","扬州市","苏州市","镇江市","其他"];
citys["广东省"]=["广州市","珠海市","其他市"];
citys["福建省"]=["福州市","厦门市","漳州市","泉州市","三明市","其他"];
citys['深圳市'] = ['福田区', '罗湖区', '盐田区', '宝安区', '龙岗区', '南山区', '深圳周边'];
citys['重庆市'] = ['俞中区', '南岸区', '江北区', '沙坪坝区', '九龙坡区', '渝北区', '大渡口区', '北碚区'];
citys['天津市'] = ['和平区', '河西区', '南开区', '河北区', '河东区', '红桥区', '塘古区', '开发区'];
citys['浙江省'] = ['杭州市','宁波市','温州市'];
citys['四川省'] = ['其他','成都市'];
citys['海南省'] = ['海口市'];
citys['江西省'] = ['其他','南昌市'];
citys['广西省'] = ['柳州市','南宁市'];
citys['安徽省'] = ['其他','合肥市'];
citys['河北省'] = ['邯郸市','石家庄市'];
citys['河南省'] = ['郑州市','洛阳市'];
citys['湖北省'] = ['武汉市','宜昌市'];
citys['湖南省'] = ['其他','长沙市'];
citys['陕西省'] = ['其他','西安市'];
citys['山西省'] = ['长治市','太原市'];
citys['黑龙江省'] = ['其他','哈尔滨市'];
citys['新疆省']=['乌鲁木齐市','伊犁市','其他'];
function initPro()
{
var pro=document.getElementById("province");
pro.options.length=0;
for(var c in citys)
{
var op=new Option(c,c);
pro.add(op);
}
addCity();
}
function addCity()
{
var pro=document.getElementById("province");
var n=pro.value;
var city=citys[n];
var ct=document.getElementById("city");
ct.options.length=0;
for(var i=0;i<city.length;i++)
{
var op=new Option(city[i],city[i]);
ct.add(op);
}
}
这个是原先我写的一个省市级联的js代码你可以参考一下
var citys=new Array();
citys["山东省"]=["济南市","青岛市","潍坊市","烟台市","威海市","滨州市","其他"];
citys["江苏省"]=["南京市","扬州市","苏州市","镇江市","其他"];
citys["广东省"]=["广州市","珠海市","其他市"];
citys["福建省"]=["福州市","厦门市","漳州市","泉州市","三明市","其他"];
citys['深圳市'] = ['福田区', '罗湖区', '盐田区', '宝安区', '龙岗区', '南山区', '深圳周边'];
citys['重庆市'] = ['俞中区', '南岸区', '江北区', '沙坪坝区', '九龙坡区', '渝北区', '大渡口区', '北碚区'];
citys['天津市'] = ['和平区', '河西区', '南开区', '河北区', '河东区', '红桥区', '塘古区', '开发区'];
citys['浙江省'] = ['杭州市','宁波市','温州市'];
citys['四川省'] = ['其他','成都市'];
citys['海南省'] = ['海口市'];
citys['江西省'] = ['其他','南昌市'];
citys['广西省'] = ['柳州市','南宁市'];
citys['安徽省'] = ['其他','合肥市'];
citys['河北省'] = ['邯郸市','石家庄市'];
citys['河南省'] = ['郑州市','洛阳市'];
citys['湖北省'] = ['武汉市','宜昌市'];
citys['湖南省'] = ['其他','长沙市'];
citys['陕西省'] = ['其他','西安市'];
citys['山西省'] = ['长治市','太原市'];
citys['黑龙江省'] = ['其他','哈尔滨市'];
citys['新疆省']=['乌鲁木齐市','伊犁市','其他'];
function initPro()
{
var pro=document.getElementById("province");
pro.options.length=0;
for(var c in citys)
{
var op=new Option(c,c);
pro.add(op);
}
addCity();
}
function addCity()
{
var pro=document.getElementById("province");
var n=pro.value;
var city=citys[n];
var ct=document.getElementById("city");
ct.options.length=0;
for(var i=0;i<city.length;i++)
{
var op=new Option(city[i],city[i]);
ct.add(op);
}
}
这个是原先我写的一个省市级联的js代码你可以参考一下
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询