有两种方法,一种是在Controller中传值给画面,一种是在js中给画面控件赋值。代码如下(springBoot+thymeleaf+html)
common.css
.data_style
{
text-align:center;
}
.table-cell {
height:40px;
display:table-cell;
vertical-align:middle;
}
.right-label{
padding-right:7px;
}
DateTemp.html
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>Temp画面</title>
<link href="/css/jquery-ui.css" th:href="@{/css/jquery-ui.css}" rel="stylesheet"></link>
<link href="/css/common.css" th:href="@{/css/common.css}" rel="stylesheet"></link>
<script src="/js/jquery-3.4.1.min.js" th:src="@{/js/jquery-3.4.1.min.js}"></script>
<script src="/js/jquery-ui.min.js" th:src="@{/js/jquery-ui.min.js}"></script>
<script src="/js/DateTemp.js" th:src="@{/js/DateTemp.js}"></script>
</head>
<body>
<form>
<br>
<div class="table-cell right-label">
<p style="margin-left:20px">日期范围</p>
</div>
<div class="table-cell">
<input name="DtFm" id="DtFm" type="text" class="data_style" th:value="${DtFm}" style="width:100px;text-align:center;" maxlength="10">
</div>
<div class="table-cell">
<p style="color:black;font-weight:bold;font-size:11pt;text-align:left;">~</p>
</div>
<div class="table-cell">
<input name="DtTo" id="DtTo" type="text" class="data_style" th:value="${DtTo}" style="width:100px; text-align:center;" maxlength="10">
</div>
</form>
</body>
</html>
①Controller中传值给画面
DateTempController.java
@Controller
@Scope("request")
@NotRequiredCheckSession
public class DateTempController {
@RequestMapping(value="/DateTemp", method=RequestMethod.GET)
public ModelAndView init(ModelAndView mv) {
mv.setViewName("DateTemp");
//日期范围
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
String searchDate = sdf.format(date);
//日期(FROM)
mv.addObject("DtFm", searchDate);
//日期(TO)
mv.addObject("DtTo", searchDate);
return mv;
}
}
②JS中赋值
DateTemp.js
$(function(){
//日期From不能大于日期To
//日期From下拉框设置
$("#DtFm").datepicker({
changeMonth: true,
numberOfMonths: 2,
dateFormat: "y/mm/dd",
onClose: function(pDate) {
$("#DtTo").datepicker("option", "minDate", pDate);
}
});
//日期To下拉框设置
$("#DtTo").datepicker({
changeMonth: true,
numberOfMonths: 2,
dateFormat: "y/mm/dd",
onClose: function(pDate) {
$("#DtFm").datepicker("option", "maxDate", pDate);
}
});
$("#DtFm").datepicker('setDate', new Date());
$("#DtTo").datepicker('setDate', new Date());
});
结果: