之前有过记录,select下拉联动 (Bootstrap、JQuery插件之cxselect)区别在这个级联性更强。
HTML源码:
<!DOCTYPE html> <head> <meta charset="utf-8"> <title></title> </head> <script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript" src="https://www.jb51.net/USjs.js"></script> <body> <div> <table> <tr> <td>厂商:</td> <td><select><option>请选择</option></select></td> <td>品牌:</td> <td><select><option>请选择</option></select></td> <td>型号:</td> <td><select><option>请选择</option></select></td> <td><input type="button" value="查询" /></td> </tr> </table> </div> </body> </html>
JS(USjs.js)源码: 在转载的基础上做了修改,特别是源数据JSON格式。
$(function(){ function objInit(obj){ return $(obj).html('<option>请选择</option>'); } $.getJSON('data.json', function(json) { var arrData = json; $.each(arrData,function(pF,pV){ $('#selF').append('<option value="'+pF+'">'+pV.n+'</option>'); }); $('#selF').change(function(){ objInit('#selT'); objInit('#selC'); $.each(arrData,function(pF,pS){ if($('#selF option:selected').attr('value')==pF){ $.each(pS.s,function(pT,pC){ $('#selT').append('<option value="'+pT+'">'+pC.n+'</option>'); }); $('#selT').change(function(){ objInit('#selC'); $.each(pS.s,function(pT,pC){ if($('#selT option:selected').attr('value')==pT){ $.each(pC.s,function(ii,vv){ $('#selC').append('<option value="'+ii+'">'+vv.n+'</option>'); }) } }) }); } }) }); }); //getJSON });
JSON(data.json)文件
[ { "n":"厂商1", "s":[ { "n":"品牌一", "s":[{"n":"型号1-1-1"},{"n":"型号1-1-2"}] }, { "n":"品牌二", "s":[{"n":"型号1-2-1"},{"n":"型号1-2-2"}] } ] }, { "n":"厂商2", "s":[ { "n":"品牌一", "s":[{"n":"型号2-1-1"},{"n":"型号2-1-2"}] }, { "n":"品牌二", "s":[{"n":"型号2-2-1"},{"n":"型号2-2-2"}] } ] }, { "n":"厂商3", "s":[ { "n":"品牌一", "s":[{"n":"型号3-1-1"},{"n":"型号3-1-2"}] }, { "n":"品牌二", "s":[{"n":"型号3-2-1"},{"n":"型号3-2-2"}] } ] } ]
Find more from: //www.jb51.net/article/102040.htm