欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

JS实现下拉框的动态添加(附效果)

时间:2021-07-27 08:17:57|栏目:JavaScript代码|点击:

效果展示:

页面初加载时:     


选择车类型后:     


选择车颜色后:    


JS实现下拉框的动态添加,网页代码如下

复制代码 代码如下:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>动态添加下拉框</title>
<script type="text/javascript" src="../JS文件/jquery.js"></script>
<script type="text/javascript" src="../JS文件/SelectMenu2.js" charset=“utf-8”></script>
</head>
<body>
<form action="#">
<br />
<br />
<br />
<div class="CarInfo">

<span class="CarType">车类型:
<select >
<option value="请选择" selected="selected">请选择</option>
<option value="宝马">宝马</option>
<option value="奔驰">奔驰</option>
</select>
</span>

<span class="CarColor" style="display:none">车颜色:
<select>
</select>
</span>

<span class="CarWheel" style="display:none">车轮:
<select>
</select>
</span>
</div>
</form>
</body>
</html></SPAN>

JS代码如下
复制代码 代码如下:

// JavaScript Document
$(document).ready(function (){
var CarTypeSelect = $(".CarType").children("select");
var CarColorSelect = $(".CarColor").children("select");
var CarWheelSelect = $(".CarWheel").children("select");


//给第一个下拉框的SelectChanged时间编码
CarTypeSelect.change(function (){

//取得当前下拉框的值
var CarTypeValue = $(this).val();
//当第一个下拉框内容改变的时候,第三个下拉框要隐藏起来
CarWheelSelect.parent().hide();

if(CarTypeValue !="")
{
CarColorSelect.html("");
$("<option value = ''>请选择</option>").appendTo(CarColorSelect);
switch(CarTypeValue){

case "宝马":
var CarColor=["绿色","黑色"];
for(var i = 0;i<CarColor.length;i++){
$("<option value='"+CarColor[i]+"'>" + CarColor[i] + "</option>").appendTo(CarColorSelect);
}
break;
case "奔驰":
var CarColor = ["白色","红色"];
for(var i = 0;i<CarColor.length;i++){
$("<option value='"+CarColor[i]+"'>" + CarColor[i] + "</option>").appendTo(CarColorSelect);
}
break;
}
CarColorSelect.parent().show();
}
else
{
CarColorSelect.parent().hide();
}
});

CarColorSelect.change(function (){

var CarColorValue = $(this).val();
CarWheelSelect.html("");
if(CarColorValue != "")
{
CarWheelSelect.html("");
$("<option value = ''>请选择</option>").appendTo(CarWheelSelect);
switch(CarColorValue){

case "绿色":
var CarWheel = ["绿钢","绿碳纤维"];
for(var i = 0;i<CarWheel.length;i++){
$("<option value='"+CarWheel[i]+"'>" + CarWheel[i] + "</option>").appendTo(CarWheelSelect);
}
break;
case "红色":
var CarWheel = ["红钢","红碳纤维"];
for(var i = 0;i<CarWheel.length;i++){
$("<option value='"+CarWheel[i]+"'>" + CarWheel[i] + "</option>").appendTo(CarWheelSelect);
}
break;
case "黑色":
var CarWheel = ["黑钢","黑碳纤维"];
for(var i = 0;i<CarWheel.length;i++){
$("<option value='"+CarWheel[i]+"'>" + CarWheel[i] + "</option>").appendTo(CarWheelSelect);
}
break;
case "白色":
var CarWheel = ["白钢","白碳纤维"];
for(var i=0;i<CarWheel.length;i++){
$("<option vaiue='"+CarWheel[i]+"'>" + CarWheel[i] + "</option>").appendTo(CarWheelSelect);
}
break;
}
CarWheelSelect.parent().show();
}
else
{
CarWheelSelect.parent().hide();
}
});
});

上一篇:Bootstrap中datetimepicker使用小结

栏    目:JavaScript代码

下一篇:js 数据类型判断的方法

本文标题:JS实现下拉框的动态添加(附效果)

本文地址:http://www.codeinn.net/misctech/161698.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有