(JavaScript)案例一:二级联动
2015-09-02 21:47
651 查看
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
var arr1 = ["选择英雄类型","战士","法师","坦克","射手"];
arr2 = [
['lol'],
["潘森","奥拉夫"],
["卡牌","流浪"],
["狗熊","龙龟"],
["艾希","vn"],
];
window.onload = function select(){
obj1st = document.getElementById('select1');
obj1st.length = arr1.length;
for (i=0;i<arr1.length ;i++ )
{
obj1st.options[i].text = arr1[i];
}
obj1st.onchange = function select2(){
var obj2nd = document.getElementById('select2');
obj2nd.length = arr2[obj1st.selectedIndex].length;
for (j=0;j<arr2[obj1st.selectedIndex].length ;j++ )
{
obj2nd.options[j].text = arr2[obj1st.selectedIndex][j];
}
}
}
</script>
</head>
<body>
<select id="select1" onclick="select()"></select>
<select id="select2" onchange="select2()"></select>
</body>
</html>
效果如图:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
var arr1 = ["选择英雄类型","战士","法师","坦克","射手"];
arr2 = [
['lol'],
["潘森","奥拉夫"],
["卡牌","流浪"],
["狗熊","龙龟"],
["艾希","vn"],
];
window.onload = function select(){
obj1st = document.getElementById('select1');
obj1st.length = arr1.length;
for (i=0;i<arr1.length ;i++ )
{
obj1st.options[i].text = arr1[i];
}
obj1st.onchange = function select2(){
var obj2nd = document.getElementById('select2');
obj2nd.length = arr2[obj1st.selectedIndex].length;
for (j=0;j<arr2[obj1st.selectedIndex].length ;j++ )
{
obj2nd.options[j].text = arr2[obj1st.selectedIndex][j];
}
}
}
</script>
</head>
<body>
<select id="select1" onclick="select()"></select>
<select id="select2" onchange="select2()"></select>
</body>
</html>
效果如图:
相关文章推荐
- JavaScript学习笔记4-闭包
- JavaScript学习笔记-JavaScript声明全局变量三种方式的异同
- bzoj1028 [JSOI2007]麻将
- JS 案例-添加节点
- JS-进阶-JS DOM常用的节点操作
- JavaScript中单引号和双引号的区别
- JSP标签库
- 关于JavaScript中没有块级作用域的理解
- JS使用DOM对元素进行添加和修改
- JavaScript简介
- $.getJSON有参数长度限制
- js注册实现
- XML 和 JSON 解析数据
- JS正则表达式的简单使用
- 读取JSON值
- 【JS/读书随笔】JavaScript编程精解/Eloquent JavaScript:Chapter 2 函数
- seajs的那些事儿
- JSON对象转字符串
- jsp输入框传值的问题,做个记录
- jsp如何调整默认编码