当我使用JQuery
datapicker
getDate
时,它会返回null。
我需要两个datepickers
,第二个应该小于第一个var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = d.getFullYear() + '-' +
(month<10 ? '0' : '') + month + '-' +
(day<10 ? '0' : '') + day;
var startDate;
$( function() {
$( "#start-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:'2016-01-01',
maxDate:today,
});
startDate =$( "#start-date" ).datepicker("getDate");
if(startSDate==null){
startSDate = '2016-01-01'
}
$( "#end-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:startDate,
maxDate:today,
});
} );
。
JS文件是这样的:
@Entity
@Table(name = "MY_TABLE")
publuc class MyEntity implements Serializable {
@Id
@Column(name = "ID")
private String id;
@Temporal(TemporalType.DATE)
@Column(name = "MY_DATE", nullable = true)
private Date myDate;
// other fields...
// getters and setters
}
然而,这不起作用。这是jsFiddle。
答案 0 :(得分:2)
请使用以下代码。
'use strict';
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = d.getFullYear() + '-' +
(month<10 ? '0' : '') + month + '-' +
(day<10 ? '0' : '') + day;
var fullSDate;
$( function() {
$( "#start-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:'2016-05-12',
maxDate:today,
});
$( "#end-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:fullSDate,
maxDate:today
});
$("#end-date").focusin(function(){
fullSDate =$( "#start-date" ).datepicker("getDate");
if(fullSDate==null){
fullSDate = '2016-01-01'
}
$( "#end-date" ).datepicker( "option", "minDate", fullSDate);
});
} );
请参阅此Fiddle
答案 1 :(得分:1)
它是null,因为你在jquery文件加载时第一次分配startDate变量,并且当你试图在$(&#34; #start-date&#34;)控件中没有日期时得到约会。
你应该在$(&#34; #start-date&#34;)。datepicker onselect事件上分配你的startDate变量
var startDate;
$("#start-date").datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:'2016-01-01',
maxDate:today,
onSelect: function () {
startDate =$("#start-date" ).datepicker("getDate");
}
});
$("#end-date").datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:startDate,
maxDate:today
});