通过选择一个下拉框的值去触发另一下拉框的值第二种方法

完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>通过选择一个下拉框的值去触发另一下拉框的值</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
    .product-description {
        margin-top: 20px;
        font-size: 1.2em;
    }
</style>
</head>
<body>
 
<select id="type1" onchange="changeType(this)">
    <option value=""> ---请选择--- </option>
    <option value="1">一般资产</option>
    <option value="2" >固定资产</option>                       
    <option value="3">其他</option>
</select>
 
<select  id="type2">
    <option value=""> ---请选择--- </option>
    <option value="7">柜子</option>
    <option value="8" >电脑</option>                     
    <option value="9">其他</option>
</select>
 
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
 
<script>
 
    function changeType(obj){
        var type1Val=$(obj).val();
        if(type1Val == '1'){//当选择为“一般资产”时则自动选择“柜子”
            $("#type2").children("[selected='selected']").removeAttr("selected");
            var $type2List=$("#type2").children();
                for(i=1;i<$type2List.length;i++){
                    if($type2List[i].value=='7'){
                        $($type2List[i]).attr("selected","selected");
                        break;
                    }
                }
            
        }
 
    }
 
</script>
 
</body>
</html>

  

posted @   Amy清风  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示