当前位置:网站首页>级联下拉菜单的实现「建议收藏」
级联下拉菜单的实现「建议收藏」
2022-08-09 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
最近在项目中用到了级联菜单,在网上查过资料后,大概有两种实现思路:
1.下拉列表中的数据从数据库中自动获取
2.下拉列表中的每一个选项都写在代码中
相信我们都会选择第一种方法,这样不仅增强了代码的复用性,而且当我们的需求变动,需要显示新的数据时,只要更新数据库就可以了。而第二种方法只能适用于某种情况,需求改变,就需要在代码中进行大量的修改。
我们今天就先来介绍一下用第二种方法来实现级联下拉菜单的效果
<!DOCTYPE >
<html>
<head>
<title> 代码实例:级联菜单 </title>
<meta content="text/html; charset=utf-8" />
</head>
<body>
<select id="College" name="College" size="1" οnchange="Redirect(this.options.selectedIndex)">
<option value="">化学与材料科学学院</option>
<option value="">管理学院</option>
<option value="">教育学院</option>
<option value="">经济学院</option>
<option value="">其他</option>
</select>
<select id="Major" name="Major" size="1">
<option value="1">化学</option>
<option value="2">材料化学</option>
<option value="3">应用化学</option>
</select>
<script type="text/javascript">
var groups = document.getElementById("College").options.length;
var group = new Array(groups)
for (i = 0; i < groups; i ++)
group[i] = new Array()
group[0][0] = new Option("化学","40")
group[0][1] = new Option("材料化学","41")
group[0][2] = new Option("应用化学","42")
group[1][0] = new Option("财务管理本科","1")
group[1][1] = new Option("财务管理专接本","2")
group[1][2] = new Option("财务会计专科","3")
group[1][3] = new Option("财务信息管理专科","4")
group[2][0] = new Option("初等教育理本科","80")
group[2][1] = new Option("初等教育文本科","81")
group[2][2] = new Option("教育学本科","82")
group[2][3] = new Option("心理学本科","83")
group[3][0] = new Option("国际经济与贸易本科","120")
group[3][1] = new Option("市场营销本科","121")
group[3][2] = new Option("国际贸易实务专科","122")
group[3][3] = new Option("市场营销专科","123")
group[3][4] = new Option("金融与证劵专科","124")
group[4][0] = new Option("其他","160")
var temp = document.getElementById("Major");
function Redirect(x){
for (m = temp.options.length-1; m > 0; m --)
temp.options[m] = null
for (i = 0; i < group[x].length; i ++){
temp.options[i] = new Option(group[x][i].text,group[x][i].value);
}
temp.options[0].selected = true
}
</script>
</body>
</html>
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105728.html原文链接:https://javaforall.cn
边栏推荐
- 【云原生】4.2 DevOps 精讲篇
- Xiaohei's leetcode journey: 94. Inorder traversal of binary trees (supplementary Morris inorder traversal)
- PMP daily practice | didn't lost a 8.9 (including agile + multi-select)
- 自监督学习 —— MoCo v2
- [Cloud Native] 4.2 DevOps Lectures
- 【stack】【queue】【priority_queue】【deque】详解
- Word箭头上面怎么打字
- Sudoku | Backtrack-7
- 线段相交的应用
- Cookie, session, token
猜你喜欢
随机推荐
什么是源文件?
hdu 1503 Advanced Fruits(最长公共子序列的应用)
线段相交的应用
5个 Istio 访问外部服务流量控制最常用的例子,你知道几个?
Pagoda measurement - building LightPicture open source map bed system
Word怎么制作双面席卡?使用Word制作双面席卡方法
如何让您的公司内容满足 GDPR 合规性
mysql配置参数详解[通俗易懂]
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
【测试】语句覆盖,判定覆盖,条件覆盖,路径覆盖
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
Word文档怎么输入无穷大符号∞
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
Photometric Stereo 光度立体法三维重建
Shanghai Konan SmartRocket series product introduction (3): SmartRocket iVerifier computer interlocking system verification tool