3 Star 0 Fork 0

乄海有海念/ywdfb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
三级联动.html 3.51 KB
一键复制 编辑 原始数据 按行查看 历史
乄海有海念 提交于 2017-02-16 16:44 . no commit message
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
省:
<select style="width: 100px;" id="pre" onchange="chg(this);">
<option value="-1">请选择</option>
</select>
市:
<select style="width: 100px;" id="city" onchange="chg2(this)" ;>
</select>
区:
<select style="width: 100px;" id="area" onchange="chg3(this)">
</select>
<div>
<input type="hidden" id="a">
<input type="hidden" id="b">
<input type="hidden" id="c">
</div>
</body>
<script>
//声明省
var pres = ["小学", "初中", "高中"]; //直接声明Array
//声明市
var cities = [
["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"],
["初一", "初二", "初三"],
["高一", "高二", "高三"]
];
var areas = [
[
["语文", "数学", "英语"],
["语文", "数学", "英语"],
["语文", "数学", "英语"],
["语文", "数学", "英语"],
["语文", "数学", "英语"],
["语文", "数学", "英语"]
],
[
["语文", "数学", "英语","物理","化学"],
["语文", "数学", "英语","物理","化学"],
["语文", "数学", "英语","物理","化学"]
],
[
["语文", "数学", "英语","物理","化学"],
["语文", "数学", "英语","物理","化学"],
["语文", "数学", "英语","物理","化学"]
]
]
//设置一个省的公共下标
var pIndex = -1;
var preEle = document.getElementById("pre");
var cityEle = document.getElementById("city");
var areaEle = document.getElementById("area");
//先设置省的值
for (var i = 0; i < pres.length; i++) {
//声明option.<option value="pres[i]">Pres[i]</option>
var op = new Option(pres[i], i);
//添加
preEle.options.add(op);
}
function chg(obj) {
if (obj.value == -1) {
cityEle.options.length = 0;
areaEle.options.length = 0;
}
//获取值
var val = obj.value;
pIndex = obj.value;
//获取ctiry
var cs = cities[val];
//获取默认区
var as = areas[val][0];
//先清空市
cityEle.options.length = 0;
areaEle.options.length = 0;
for (var i = 0; i < cs.length; i++) {
var op = new Option(cs[i], i);
cityEle.options.add(op);
}
for (var i = 0; i < as.length; i++) {
var op = new Option(as[i], i);
areaEle.options.add(op);
}
document.getElementById('a').value=parseInt(val)+1;
document.getElementById('b').value=parseInt(document.getElementById('city').value)+1;
document.getElementById('c').value=parseInt(document.getElementById('area').value)+1;
}
function chg2(obj) {
var val = obj.selectedIndex;
var as = areas[pIndex][val];
areaEle.options.length = 0;
for (var i = 0; i < as.length; i++) {
var op = new Option(as[i], i);
areaEle.options.add(op);
}
document.getElementById('b').value=parseInt(val)+1;
document.getElementById('c').value=parseInt(document.getElementById('area').value)+1;
}
function chg3(obj) {
var val = obj.selectedIndex;
document.getElementById('c').value=parseInt(val)+1;
}
</script>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/li941226/ywdfb.git
git@gitee.com:li941226/ywdfb.git
li941226
ywdfb
ywdfb
master

搜索帮助