html 如何做一个二级联动的下拉表单(select)

html-css022

html 如何做一个二级联动的下拉表单(select),第1张

<!doctype html><html><head><meta charset="utf-8"><title>联动</title></head><body><select id="s1" onChange="move()"><!-- 根据id获取value,onchange()触发函数move()修改二级select的text值实现联动 -->

<option selected value="s1">-- 请选择 --</option>

<!--默认选中-->

<option value="SC">SC</option>

<option value="BJ">BJ</option>

<option value="TJ">TJ</option></select><select id="s2">

<option selected>-- 请选择 --</option>

<!--默认选中,动态生成option --></select><script>

function move()

{

var s1=document.getElementById("s1")var s2=document.getElementById("s2")<!-- 获取一级和二级的属性-->

var add

if(s1.value=="SC")

{

add=new Array("CD","DZ","MY","NC")<!--比对value值,实现对应二级text值的动态生成-->

}else if(s1.value=="BJ")

{

add=new Array("HD","CY","WFJ")<!--比对value值,实现对应二级text值的动态生成-->

}else if(s1.value=="TJ")

{

add=new Array("TJG","TJ","TJY")<!--比对value值,实现对应二级text值的动态生成-->

}else if(s1.value=="s1")

{

add=new Array("请选择")

}

else

{

add=null<!--若没有就为空,当然不可能出现的-->

}

s2.length=0

for(var i=0i<add.lengthi++) { var ss=new Option() ss.text=add[i].split()[0]

s2.add(ss)

<!--把text值添加到二级select中,显示出来-->

}

}</script></body></html>

按说这种操作应该是后台程序来做,前端操作的话有点窒息,主要是数据量。。亲测有效,望采纳!

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>无标题文档</title>

<link rel="stylesheet" type="text/css" href="CSS3/xuanxing.css" />

<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script>

</head>

<body>

<style>

select{ width:75px font-size:12px color:#000000 font-family:'宋体' margin:auto 0px}

#select0{ background:yellow}

.select{ background:#85e785}

</style>

<script>

$(document).ready(function(e) {

$("select#select0").change(function(){

var cc = $(this).find("option:selected").attr("class")

$(this).siblings("select").find("option").hide()

$(this).siblings("select").find("option."+cc).show()

$(this).siblings("select").find("option."+cc+":first").attr("selected","selected")

})

$("select.select").change(function(){

var cid = $(this).find("option:selected").index()

$(this).siblings("select.select").each(function(){

$(this).find("option").eq(cid).attr("selected","selected")

})

})

})

</script>

<div style="width: 400px height: 20px margin:0 auto word-spacing: 0px">

<select id="select0">

<option class="op0">10mm</option>

<option class="op1">20mm</option>

<option class="op2">40mm</option>

</select>

<select class="select">

<option class="op0">8n</option>

<option class="op0">12n</option>

<option class="op0">16n</option>

<option class="op1">24n</option>

<option class="op1">28n</option>

<option class="op1">32n</option>

<option class="op2">40n</option>

<option class="op2">44n</option>

</select>

<select class="select">

<option class="op0">70mm</option>

<option class="op0">110mm</option>

<option class="op0">150mm</option>

<option class="op1">230mm</option>

<option class="op1">270mm</option>

<option class="op1">310mm</option>

<option class="op2">390mm</option>

<option class="op2">430mm</option>

</select>

<select class="select">

<option class="op0">128mm</option>

<option class="op0">190mm</option>

<option class="op0">230mm</option>

<option class="op1">310mm</option>

<option class="op1">350mm</option>

<option class="op1">390mm</option>

<option class="op2">470mm</option>

<option class="op2">510mm</option>

</select>

<select class="select">

<option class="op0">ESA0810</option>

<option class="op0">ESA1210</option>

<option class="op0">ESA1610</option>

<option class="op1">ESA2410</option>

<option class="op1">ESA2810</option>

<option class="op1">ESA3210</option>

<option class="op2">ESA4010</option>

<option class="op2">ESA4410</option>

</select>

</div>

</body>

</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" lang="gb2312">

<head>

<head>

<title>代码实例:级联菜单 </title>

<meta http-equiv="content-type" content="text/htmlcharset=gb2312" />

<meta name="author" content="枫岩,CNLei.y.l@gmail.com">

</head>

<body>

<select id="BigCate" name="BigCate" size="1" onchange="Redirect(this.options.selectedIndex)">

<option value="">全市</option>

<option value="">主城区</option>

<option value="">分公司</option></select>

<select id="SmlCate" name="SmlCate" size="1">

<option value="1">全市</option>

</select>

<script type="text/javascript">

<!--

var groups = document.getElementById("BigCate").options.length

var group = new Array(groups)

for (i = 0i <groupsi ++)

group[i] = new Array()group[0][0] = new Option("全市","ALL")group[1][0] = new Option("北部新区局","CJ0")

group[1][1] = new Option("杨家坪局","CY0")

group[1][2] = new Option("沙坪坝局","CP0")

group[1][3] = new Option("观音桥局","CG0")

group[1][4] = new Option("上清寺局","CS0")

group[1][5] = new Option("南坪局","CN0")group[2][0] = new Option("渝北电信公司","CF0")

group[2][1] = new Option("璧山电信公司","CA0")

group[2][2] = new Option("江津电信公司","CU0")

group[2][3] = new Option("合川电信公司","CK0")

group[2][4] = new Option("北碚电信公司","CI0")group[2][5] = new Option("巴南电信公司","CB0")

group[2][6] = new Option("永川电信公司","CH0")

group[2][7] = new Option("万州电信公司","WW0")

var temp = document.getElementById("SmlCate")

function Redirect(x){ for (m = temp.options.length-1m >0m --)

temp.options[m] = null

for (i = 0i <group[x].lengthi ++){

temp.options[i] = new Option(group[x][i].text,group[x][i].value)

alert(group[x][i].value)

}

temp.options[0].selected = true

}

//-->

</script></body>

</html>这个代码是入门的最佳辅助代码