现在要用Jquery UI 的datepicker做一个时间段的选择,要求选择了startDate后,endDate要在startDate之前;选择endDate后,startDate要在endDate之前。
使用代码:
$("#startDate").datepicker({
onSelect:function(dateText,inst){
$("#endDate").datepicker("option","minDate",dateText);
},
timeFormat : 'hh:mm:ss',
dateFormat : 'yy-mm-dd',
});
$("#endDate").datepicker({
onSelect:function(dateText,inst){
$("#startDate").datepicker("option","maxDate",dateText);
},
timeFormat : 'hh:mm:ss',
dateFormat : 'yy-mm-dd',
});
可以实现,但是当手动清除日期后,所选择的时间范围会出现问题:
比如: 刚开始我们选择startDate为20150714,endDate为20150729,然后手动清空startDate与endDate的日期,再次点击选择日期时,它们仍然有日期范围限制:
startDate:
endDate:
我现在找到一个办法处理的是: 给它们添加onClose事件,在它close时调用方法:$.datepicker._clearDate(this);
_.clearDate()是在DatePicker的对象。你不会找到它的jQuery UI网站上的公共API,但是它的工作原理就像一个charisma。(参考了这篇文章:http://codego.net/422121/)
$("#startDate").datepicker({
onSelect:function(dateText,inst){
$("#endDate").datepicker("option","minDate",dateText);
},
onClose: function() {
if ($("#startDate").val() == "") {
$.datepicker._clearDate(this);
}
},
timeFormat : 'hh:mm:ss',
dateFormat : 'yy-mm-dd',
});
$("#endDate").datepicker({
onSelect:function(dateText,inst){
$("#startDate").datepicker("option","maxDate",dateText);
},
onClose: function() {
if ($("#endDate").val() == "") {
$.datepicker._clearDate(this);
}
},
timeFormat : 'hh:mm:ss',
dateFormat : 'yy-mm-dd',
});
暂时就只找到这个处理方法,必须要控件close的时候才会触发,不知道能不能在清空日期的时候就立马将日期限制去掉,知道的大神可以给个解决方案不