我的日期选择器有一个错误,如果您没有选择或选择一年,例如用户选择May 25, 1935
(1935年是最低和默认年份)。在文本框中,它将显示为2015/05/25
或2015年5月25日。
这是代码,因此您可以看到错误。
$(function() {
var startDate = "1935";
var endDate = new Date().getFullYear() - 18;
var interval = startDate + ":" + endDate;
$("#from2").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: interval,
onClose: function(selectedDate) {
$("#to").datepicker("option", "minDate", selectedDate);
}
});
$("#to2").datepicker({
defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: '1935:' + (new Date).getFullYear(),
onClose: function(selectedDate) {
$("#from2").datepicker("option", "maxDate", selectedDate);
}
});
});

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<label for="from">Birthday</label>
<input type="text" id="from2" name="from">
</body>
</html>
&#13;
答案 0 :(得分:3)
您正在做的是将defaultDate
设置为"+1w"
,这意味着&#34;添加一周到今天&#34; 。你可能应该这样做:
defaultDate: "1/1/1935"
或者您可以在实例化datepicker后使用正确的setter方法设置默认日期:
$('#my-datepicker').datepicket(/* instantiation options */)
.datepicker( "setDate", YOUR_DEFAULT_DATE ); // use setter
这是两个例子:
$(function() {
var startDate = "1935";
var endDate = new Date().getFullYear() - 18;
var interval = startDate + ":" + endDate;
// use default date
$("#one").datepicker({
defaultDate: "1/1/1935", // set default date
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: interval
});
// use setter
$("#two").datepicker({
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
yearRange: interval
}).datepicker("setDate", "1/1/1935"); // set default date
});
&#13;
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<label for="one">Birthday</label>
<input type="text" id="one" name="one">
<label for="two">Birthday 2</label>
<input type="text" id="two" name="two">
</body>
</html>
&#13;