首先声明这2个无刷新级联下拉框的jquery插件完全是自己原创的,经过严格的测试,正确使用不会出现bug。灵活性方面考虑了比较多的方面,提供了几个重要的配置方便在各类环境下使用,欢迎各位童鞋使用,源码完全开放。开发这个插件的缘于前段时间维护一个4级级联下拉框被里面200行代码及复杂的结构和bug所郁闷(之所以这么多代码是因为该级联下拉框有时只出现2个或3个),想到这类的需求其实经常都能遇到,jquery里没有这样比较好的插件,索性自己开发个。源代码并不复杂,稍微复杂的地方在第二个插件使用了缓存,造成理解起来十分困难,后面会做些解释。
插件一:适合在不与服务器进行AJAX交互情况使用,需预先将所有下拉框数据全部读出
插件二:适用于每个子级下拉框都post到服务器中取数据绑定。优秀之处在于会将已使用过的数据缓存达到高效率的目的,注意:缓存的键值不仅仅是父下拉框的值,而是从顶级下拉框到当前父下拉框的值组合,这是为了对付出现相同父下拉框对应的子级并不相同的情况。同样的原因,postback中回发给服务器的form表单中也是包括所有的父下拉框的值。
不知道怎么添加附件,把测试代码也一并贴上来,因为插件二需要服务器端的配合这里就不贴插件二的测试代码。
<!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 runat="server"> <title>无限极级联下拉框的封装</title> <script type="text/javascript" src="http://test.fe.ecp.mic.cn/content/scripts/base/jquery.js"></script> <style> select { margin-left: 10px; } body { font-size: 14px; background-color: #CCCCCC; } td { border: 1px solid #FF6600; padding: 5px; text-align: left; } input { width: 80px; } input[type=checkbox] { width: 20px; } </style> </head> <body> <form id="form1" runat="server"> <div> <h1> 无限极级联下拉框的封装</h1> <div style="margin: 50px 0 50px 10px;"> 绑定方法:<select id="selCase"><option value="1">第一种方法</option> <option value="2">第二种方法</option> </select> <input type="button" onclick="test()" value="绑定" /> <div style="margin: 10px;"> <table cellpadding="0" cellspacing="0"> <tr> <td> 第一个下拉框: </td> <td> <input type="text" id="tb1sel" value="a002" />设置当前项的选中值 </td> <td> </td> <td> </td> </tr> <tr> <td> 第二个下拉框: </td> <td> <input type="text" id="tb2sel" value="" />设置当前项的选中值 </td> <td> <input type="checkbox" id="cb2Remove" value="" />是否移除第一项 </td> <td> <input type="text" id="tb2AllValue" value="0" />当前一项值等于此值时,该项全选 </td> </tr> <tr> <td> 第三个下拉框: </td> <td> <input type="text" id="tb3sel" value="" />设置当前项的选中值 </td> <td> <input type="checkbox" id="cb3Remove" />是否移除第一项 </td> <td> <input type="text" id="tb3AllValue" value="" />当前一项值等于此值时,该项全选 </td> </tr> </table> </div> </div> <h4> 下拉框结果:</h4> <div id="selContainer" style="margin: 0px 0 50px 100px;"> </div> <script type="text/javascript" src="/Scripts/Jquery.cascadeDropDown-1.2.js"></script> <script type="text/javascript"> $(function () { toggleSetting(); $('#selCase').bind('change', toggleSetting); }); function toggleSetting() { if ($('#selCase').val() == '1') $('table tr').each(function (i, item) { $($(item).find('td')[3]).hide(); }); else $('table tr').each(function (i, item) { $($(item).find('td')[3]).show(); }); } function test() { if ($('#selCase').val() == '1') testcase1(); else testcase2(); } function testcase1() { $('#Select1').remove(); $('#Select2').remove(); $('#Select3').remove(); $('#selContainer').html('<select id="Select1"><option value="-1">全部</option></select><select id="Select2"><option value="-1">全部</option></select><select id="Select3"><option value="-1">全部</option></select>'); var dataItem = [['a001', 'a001', '0'], ['a002', 'a002', '0'], ['a003', 'a003', '0'] , ['b001', 'b001', 'a001'], ['b002', 'b002', 'a001'], ['b003', 'b003', 'a002'], ['b004', 'b004', 'a002'], ['b005', 'b005', 'a003'] , ['c001', '001', 'b001'], ['c002', '002', 'b001'], ['c003', '003', 'b002'], ['c004', '004', 'b002'], ['c005', '005', 'b003'], ['c006', '006', 'b004'], ['c007', '007', 'b004'], ['c008', '008', 'b005'] ]; $.cascadeDropDownBind.bind(dataItem, { sourceID: 'Select1', selectValue: $('#tb1sel').val(), parentValue: '0', removeFirst: false, child: { sourceID: 'Select2', selectValue: $('#tb2sel').val(), removeFirst: $('#cb2Remove').attr('checked'), child: { sourceID: 'Select3', selectValue: $('#tb3sel').val(), removeFirst: $('#cb3Remove').attr('checked') } } } ); } function testcase2() { $('#Select1').remove(); $('#Select2').remove(); $('#Select3').remove(); //$('#selContainer').html('<select id="Select1"></select><select id="Select2"></select><select id="Select3"></select>'); $('#selContainer').html('<select id="Select1"><option value="0">全部</option></select><select id="Select2"><option value="0">全部</option></select><select id="Select3"><option value="0">全部</option></select>'); var data = [['a001', 'a001'], ['a002', 'a002'], ['a003', 'a003']]; var data2 = [['b001', 'b001', 'a001'], ['b002', 'b002', 'a001'], ['b003', 'b003', 'a002'], ['b004', 'b004', 'a002'], ['b005', 'b005', 'a003']]; var data3 = [['c001', '001', 'b001'], ['c002', '002', 'b001'], ['c003', '003', 'b002'], ['c004', '004', 'b002'], ['c005', '005', 'b003'], ['c006', '006', 'b004'], ['c007', '007', 'b004'], ['c008', '008', 'b005']]; $.cascadeDropDownBind.bind(data, { sourceID: 'Select1', selectValue: $('#tb1sel').val(), removeFirst: false }); $.cascadeDropDownBind.bind(data2, { sourceID: 'Select2', selectValue: $('#tb2sel').val(), parentID: 'Select1', removeFirst: $('#cb2Remove').attr('checked'), appentAllValue: $('#tb2AllValue').val() }); $.cascadeDropDownBind.bind(data3, { sourceID: 'Select3', selectValue: $('#tb2sel').val(), parentID: 'Select2', removeFirst: $('#cb3Remove').attr('checked'), appentAllValue: $('#tb3AllValue').val() }); } </script> </div> </form> </body> </html>