今天项目中用到了联动的下拉框,这个是经常用的小技巧,就记录了下来,界面如下。

选择前如下:

选择中:

选择后下拉框都是联动的,4个联动下拉框:

 

jsp页面代码如下:

 

 
  
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
  4. <%@ taglib uri="/struts-tags" prefix="s" %> 
  5. <
  6.     String ctx = request.getContextPath(); 
  7.     pageContext.setAttribute("ctx", ctx); 
  8. %> 
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  10. <html> 
  11. <head> 
  12. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  13. <script type="text/javascript" src="<%=ctx %>/js/jquery.js" ></script> 
  14. <script type="text/javascript" src="<%=ctx%>/ckeditor/ckeditor.js"></script> 
  15. <script type="text/javascript" src="<%=ctx%>/ckfinder/ckfinder.js"></script> 
  16. <title>商家</title> 
  17. <link href="<%=ctx %>/css/class.css" rel="stylesheet" type="text/css"> 
  18. <script type="text/javascript" src="<%=ctx %>/js/My97DatePicker/WdatePicker.js"></script> 
  19. <script type="text/javascript"> 
  20. function checkFile() 
  21. {  
  22.     //获得选择的文件的URL 
  23.     var fileURL = document.forms1.logoUplode.value; 
  24.     //获得文件的扩展名 
  25.     fileURLfileURLSplit = fileURL.split("."); 
  26.     fileExt = fileURLSplit[fileURLSplit.length-1].toLowerCase(); 
  27.     alert(fileURL); 
  28.     //判断是否是图片文件 
  29.     if (fileExt=="jpg" || fileExt=="bmp" || fileExt=="gif" || fileExt=="png") 
  30.     { 
  31.         //var obj = document.getElementById('lookpic'); 
  32.         //obj.src = fileURL
  33.         return true; 
  34.     } 
  35.     else 
  36.     { 
  37.         alert("请选择图片文件"); 
  38.         return false; 
  39.     } 
  40. }    
  41. function onValidate() { 
  42.     //提交保存 
  43.     //验证 
  44.     var dealerId = document.getElementById("dealerId").value; 
  45.     var modelId = document.getElementById("tbDealerSales.modelId").value; 
  46.     var modelId2 = document.getElementById("tbDealerSales.modelId2").value; 
  47.     if(dealerId==""){ 
  48.         document.getElementById("message").innerHTML = "请选择商家!"
  49.         return false; 
  50.     } 
  51.     if(modelId==""){ 
  52.         document.getElementById("message").innerHTML = "请选择车型!"
  53.         return false; 
  54.     } 
  55.     if(modelId2==""){ 
  56.         document.getElementById("message").innerHTML = "请选择具体车型,如果没有具体车型请在车型管理中添加"
  57.         return false; 
  58.     } 
  59.     if(trim(document.getElementById("tbDealerSales.salesPrice").value)=="") { 
  60.         document.getElementById("message").innerHTML = "报价不能为空!"
  61.         return false; 
  62.     } 
  63.     if(trim(document.getElementById("validStime").value)=="") { 
  64.         document.getElementById("message").innerHTML = "开始日期不能为空!"
  65.         return false; 
  66.     } 
  67.      if(trim(document.getElementById("validEtime").value)=="") { 
  68.         document.getElementById("message").innerHTML = "结束日期不能为空!"
  69.         return false; 
  70.     } 
  71.     return true; 
  72. function add(){ 
  73.     if(onValidate()){ 
  74.         document.forms[0].action="<%=ctx%>/priceAction!add.action"
  75.         document.forms[0].submit();  
  76.     } 
  77. function trim(str){  
  78.     return str.replace(/(^\s*)|(\s*$)/g, "");  
  79. }  
  80. function refreshCarBrand(val){ 
  81.     document.getElementById('tbDealerSales.modelId').options.length = 0
  82.     carBrand = document.getElementById('carBrand').value; 
  83.     //alert(carBrand); 
  84.     var str = $.ajax({ 
  85.         url: '<%=ctx%>/priceAction!getCarModel.action?carBrand=' + val + '&now=' + new Date().getTime(), 
  86.         async: false 
  87.         }).responseText; 
  88.     //str = "[{'name':'1','value':'1'},{'name':'2','value':'2'}]"
  89.    // alert(str); 
  90.     str = eval("(" + str + ")"); 
  91.      
  92.     for (var i = 0; i < str.length; i++){ 
  93.         //alert(str[i].value); 
  94.         document.getElementById('tbDealerSales.modelId').options[i] = new Option(str[i].name,str[i].value); 
  95.     } 
  96.     if(document.getElementById('tbDealerSales.modelId').options.length == 0){ 
  97.         document.getElementById('tbDealerSales.modelId').options[i] = new Option("--暂无数据--",""); 
  98.         refreshCarBasic("99999"); 
  99.     }else{ 
  100.         refreshCarBasic(str[0].value); 
  101.     } 
  102. function refreshCarBasic(val){ 
  103.     document.getElementById('tbDealerSales.modelId2').options.length = 0
  104.     carBrand = document.getElementById('tbDealerSales.modelId').value; 
  105.     //alert(carBrand); 
  106.     var str = $.ajax({ 
  107.         url: '<%=ctx%>/priceAction!getCarDetail.action?carId=' + val + '&now=' + new Date().getTime(), 
  108.         async: false 
  109.         }).responseText; 
  110.     //str = "[{'name':'1','value':'1'},{'name':'2','value':'2'}]"
  111.     str = eval("(" + str + ")"); 
  112.     for (var i = 0; i < str.length; i++){ 
  113.         //alert(str[i].value); 
  114.         document.getElementById('tbDealerSales.modelId2').options[i] = new Option(str[i].name,str[i].value); 
  115.     } 
  116.     if(document.getElementById('tbDealerSales.modelId2').options.length == 0){ 
  117.         document.getElementById('tbDealerSales.modelId2').options[i] = new Option("--暂无数据--",""); 
  118.     } 
  119.  
  120. function init(){ 
  121.     //refreshCarBrand(document.getElementById('carBrand').value); 
  122.     //refreshCarBasic(document.getElementById('tbDealerSales.modelId').value); 
  123.  
  124. function selectCarBrand(val){ 
  125.     //alert(val); 
  126.     var str = $.ajax({ 
  127.         url: '<%=ctx%>/priceAction!getBand.action?dealerId=' + val , 
  128.         async: false         
  129.     }).responseText; 
  130.     //alert(str); 
  131.     var s = ""
  132.     carBrand = document.getElementById('carBrand'); 
  133.     var carBrandcarBrandlength = carBrand.options.length; 
  134.     for(var i=0;i<carBrandlength;i++){ 
  135.         if (carBrand.options[i].value == str)       {     
  136.             carBrand.options[i].selected=true
  137.             break; 
  138.         }        
  139.     }  
  140.     refreshCarBrand(str); 
  141. </script> 
  142. </head> 
  143.  
  144. <body onload="init()"> 
  145.     <table width="100%"  border="0" cellspacing="0" cellpadding="0">    
  146.       <tr> 
  147.         <td width="26" height="25"><img src="<%=ctx %>/p_w_picpaths/main/main01.jpg" width="26" height="15"></td> 
  148.         <td width="62" class="bg01">当前位置:</td> 
  149.         <td class="bg01"><DIV id="u3_rtf"> 报价&gt;  <span class="red">新增报价</span></DIV></td> 
  150.       </tr> 
  151.     </table> 
  152.     <table width="100%"  border="0" cellspacing="0" cellpadding="0"> 
  153.         <tr> 
  154.           <td height="4" background="<%=ctx %>/p_w_picpaths/main/main02.gif"><img src="<%=ctx %>/p_w_picpaths/main/main02.gif" width="6" height="4"></td> 
  155.         </tr> 
  156.       </table> 
  157. <s:form name="forms1" enctype="multipart/form-data" method="post"> 
  158. <table border="0" width="70%"  border="0" cellspacing="0" cellpadding="0"> 
  159.     <tr> 
  160.         <td>选择商家:</td> 
  161.         <td> 
  162.             <s:select label="选择商家"  cssStyle="width:120pt;"   id="dealerId" name="tbDealerSales.dealerId" list="tbDealerInfoList" listKey="dealerId" listValue="dealerName" headerKey="" headerValue="----请选择商家----" theme="simple" onchange="selectCarBrand(this.value)"/> 
  163.             <font color="red">*</font> 
  164.         </td> 
  165.     </tr> 
  166.     <tr> 
  167.         <td>选择品牌:</td> 
  168.         <td> 
  169.             <s:select label="选择品牌"  cssStyle="width:120pt;"  id="carBrand" name="carBrand" list="tbCarBrandList" listKey="brandId" listValue="brandName" headerKey="" headerValue="----请选择品牌----" onchange="refreshCarBrand(this.value)" theme="simple" disabled="true"/>            
  170.             <font color="red">*一级/二级品牌,品牌在商家添加的时候已经关联</font> 
  171.         </td> 
  172.     </tr> 
  173.     <tr> 
  174.         <td>选择车型:</td> 
  175.         <td> 
  176.             <s:select id="tbDealerSales.modelId"   cssStyle="width:120pt;"  name="tbDealerSales.modelId" label="选择车型" list="#{ }" headerKey="" headerValue="----请选择车型----"  onchange="refreshCarBasic(this.value)" theme="simple" /> 
  177.             <font color="red">*</font> 
  178.         </td> 
  179.     </tr> 
  180.     <tr> 
  181.         <td>选择具体车型:</td> 
  182.         <td> 
  183.             <s:select id="tbDealerSales.modelId2"   cssStyle="width:120pt;"  name="tbDealerSales.modelId2" label="具体车型" list="#{ }" headerKey="" headerValue="----请选择具体车型----" theme="simple"/> 
  184.             <font color="red">*</font> 
  185.         </td> 
  186.     </tr> 
  187.     <tr> 
  188.         <td>汽车报价:</td> 
  189.         <td> 
  190.             <input type="text" name="tbDealerSales.salesPrice" id="tbDealerSales.salesPrice" onkeyup="this.value=this.value.replace(/[^\d/.]/g,'')"  maxlength='4'/> 
  191.             万<font color="red">*</font> 
  192.         </td> 
  193.     </tr> 
  194.     <tr> 
  195.         <td>有效期:</td> 
  196.         <td> 
  197.             <input name="validStime" id="validStime" class="Wdate" type="text" onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'validEtime\')||\'2020-10-01\'}'})"/>至  
  198.             <input name="validEtime" id="validEtime" class="Wdate" type="text" onFocus="WdatePicker({minDate:'#F{$dp.$D(\'validStime\')}',maxDate:'2020-10-01'})"/>              
  199.             <font color="red">*</font> 
  200.         </td> 
  201.     </tr> 
  202.     <tr> 
  203.         <td><input  type="button" value="添加" onclick="add();"/></td> 
  204.         <td> 
  205.          <input type="button" value="取消" onclick="javascript:history.back();"/> 
  206.         <font color="red" id="message"><s:property value="#request.errorMessage"/></font> 
  207.         </td> 
  208.     </tr> 
  209. </table> 
  210. </s:form> 
  211. </body> 
  212. </html> 

后台代码如下:

 

 
  
  1. public void setCarBasic(String[] idArray){ 
  2.     List list = testService.getCarDetail(idArray[0]); 
  3.     tbCarBasicList = new ArrayList<TbCarBasic>(); 
  4.     System.out.println("tbCarBasicList.size():" + tbCarBasicList.size()); 
  5.      

获取车型代码如下:

 
  
  1.     public String getCarModel(){ 
  2.         System.out.println("carBrand--------------------" + carBrand); 
  3.         response().setCharacterEncoding("UTF-8"); 
  4.         response().setContentType("text/html;charset=UTF-8"); 
  5.         try { 
  6.             StringBuffer sb = new  StringBuffer(); 
  7.              
  8.             String str = "[{'name':'1','value':'1'},{'name':'2','value':'2'}]"
  9.             List<TbCarBasic> list = testService.getCarModels(carBrand); 
  10.             sb.append("["); 
  11.             for(int i=0;i<list.size();i++){ 
  12.                 TbCarBasic t = list.get(i); 
  13.                 if(i==0){ 
  14.                     sb.append("{'name':'"+t.getCarName()+"','value':'"+t.getCarId()+"'}"); 
  15.                 }else{ 
  16.                     sb.append(",{'name':'"+t.getCarName()+"','value':'"+t.getCarId()+"'}"); 
  17.                 } 
  18.             } 
  19.             sb.append("]"); 
  20. //          System.out.println(sb.toString()); 
  21.             response().getWriter().write(sb.toString()); 
  22.             response().getWriter().close();  
  23.         } catch (IOException e) { 
  24.             e.printStackTrace(); 
  25.         } 
  26.         return null; 
  27.     } 

获取具体车型代码如下:

 
  
  1.     public String getCarDetail(){ 
  2. //      System.out.println("--------------------"); 
  3.         response().setCharacterEncoding("UTF-8"); 
  4.         response().setContentType("text/html;charset=UTF-8"); 
  5.         try { 
  6.             StringBuffer sb = new  StringBuffer(); 
  7.              
  8.             String str = "[{'name':'1','value':'1'},{'name':'2','value':'2'}]"
  9.             List<TbCarDetail> list = testService.getCarDetail(carId); 
  10.             sb.append("["); 
  11.             for(int i=0;i<list.size();i++){ 
  12.                 TbCarDetail t = list.get(i); 
  13.                 if(i==0){ 
  14.                     sb.append("{'name':'"+t.getModelName()+"','value':'"+t.getModelId()+"'}"); 
  15.                 }else{ 
  16.                     sb.append(",{'name':'"+t.getModelName()+"','value':'"+t.getModelId()+"'}"); 
  17.                 } 
  18.             } 
  19.             sb.append("]"); 
  20.             System.out.println(sb.toString()); 
  21.             response().getWriter().write(sb.toString()); 
  22. //          response().getWriter().write(str); 
  23.             response().getWriter().close();  
  24.         } catch (IOException e) { 
  25.             e.printStackTrace(); 
  26.         } 
  27.         return null; 
  28.     }