通过选择一个下拉框的值去触发另一下拉框的值
完整代码
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <!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> // $(document).ready(function() { // // 使用 jQuery 设置下拉框的值为 'selectedValue' // $('#type1').val('2'); // // 可选:触发 change 事件(如果下拉框有绑定的事件处理器) // $('#type1').trigger('change'); // // 禁用下拉框,使其不可更改 // $('#type1').prop('disabled', true); // }); function changeType(obj){ var type1Val=$(obj).val(); if (type1Val == '1' ){ //当选择为“一般资产”时则自动选择“柜子” // 使用 jQuery 设置下拉框的值为 '7',柜子的下拉框值时“7” $( '#type2' ).val( '7' ); // 可选:触发 change 事件(如果下拉框有绑定的事件处理器) $( '#type2' ).trigger( 'change' ); // 禁用下拉框,使其不可更改 $( '#type2' ).prop( 'disabled' , true ); } else if (type1Val == '2' ){ //当选择为“固定资产”时则自动选择“电脑” // 使用 jQuery 设置下拉框的值为 '8', 电脑的下拉框值时“8” $( '#type2' ).val( '8' ); // 可选:触发 change 事件(如果下拉框有绑定的事件处理器) $( '#type2' ).trigger( 'change' ); // 禁用下拉框,使其不可更改 $( '#type2' ).prop( 'disabled' , true ); } else if (type1Val == '3' ){ //当选择为“其他”时则自动选择“其他” // 使用 jQuery 设置下拉框的值为 '9', 其他的下拉框值时“9” $( '#type2' ).val( '9' ); // 可选:触发 change 事件(如果下拉框有绑定的事件处理器) $( '#type2' ).trigger( 'change' ); // 禁用下拉框,使其不可更改 $( '#type2' ).prop( 'disabled' , true ); } } </script> </body> </html> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性