select下拉框回显的解决办法

来源:互联网 发布:json和ajax区别 编辑:程序博客网 时间:2024/06/06 12:38

在项目中:想实现点击“查询排班表”,进来右边的页面,年月自动回显成当前年月;同时,实现往前,往后,跳转都能回显,年月是下拉框的形式;


部分代码:

@RequestMapping(value = "{type}/{deptId}/{yearMonth}/{datemap}")
public String getData(@PathVariable String type, @PathVariable Long deptId, @PathVariable String yearMonth,
@PathVariable String datemap, Model model, HttpServletRequest request) throws Exception {
String nowyear = com.yuanls._comm.util.Utils.getFormatDate("yyyy");
List listYears = new ArrayList(); //年
List listMonths = new ArrayList();//月
for (int i = 2016; i < Integer.parseInt(nowyear)+2; i++) {
listYears.add(i);
}
for(int i=1;i<10;i++) {
listMonths.add("0"+i);
}
listMonths.add(10);
listMonths.add(11);
listMonths.add(12);
model.addAttribute("listYears", listYears);
model.addAttribute("listMonths", listMonths);
String selectYear = yearMonth.substring(0, yearMonth.length()-2); //当前年
String selectMonth = yearMonth.substring(4, yearMonth.length());//当前月
model.addAttribute("year1", selectYear);
model.addAttribute("month1", selectMonth);

}

jsp部分代码:通过遍历显示下拉年月;这样比较灵活;通过js来判断回显;

<!-- 年开始: -->
  <select id="year" name="csrqnf1" style="width: 60px;" class="shortselect" value="${year1}">  
                    <option value="">--年份--</option>  
                     <c:forEach items="${listYears}" var="years" varStatus="vs">    
                         <option value="${years}">${years}</option>  
                     </c:forEach>
 
            </select>年  
            <select id="month" name="csrqnf" style="width: 45px;" class="shortselect" value="${month1}">  
                     <option value="">--月份--</option>  
                     <c:forEach items="${listMonths}" var="months" varStatus="vs">    
                         <option value="${months}">${months}</option>  
                     </c:forEach>  
            </select>月
            <script type="text/javascript">  
            document.getElementById("month").value="${month1}";  
            document.getElementById("month").options[${month1}].selected= true; 
                document.getElementById("year").value="${year1}";  
                document.getElementById("year").options[${year1}].selected= true; 
            </script>
 
  <!-- 年结束: -->


补充select回显方法:

  1. <!-- 
  2.  实现select标签回显 
  3. -->  
  4. 1.<select name="curStatus"  value="${curStatus}">     
  5.   <option value="0">-请选择-</option>     
  6.   <option value="1" <c:if test="${'1' eq curStatus}">selected</c:if> ></option>     
  7.   <option value="2" <c:if test="${'2' eq curStatus}">selected</c:if> ></option>  
  8.  </select>  
  9.   
  10. 2.<s:select  list="#{0:'--请选择--',1:'处理中',2:'已完成'}" listKey="key" listValue="value" name="curStatus" value="%{curStatus}"></s:select>        
  11.    
  12. 3.<select name="curStatus"  value="${curStatus}">     
  13.   <option value="0">-请选择-</option>     
  14.   <option value="1" <s:if test="%{curStatus==1}">selected</s:if> >处理中</option>     
  15.   <option value="2" <s:if test="%{curStatus==2}">selected</s:if> >已处理</option>  
  16.  </select>  

2 0
原创粉丝点击