date time picker - 设置to 日期晚于from 日期

rdfzfanghui 2015-10-30 09:43:36
我用的是jquery datetime,但是去除了选择”日“的功能:
如以下图所示:

现在的问题是,下面的代码,第一次用没有问题。但是如果已经选择了一次from 和to 的时间,再想改变from 时间,这个时候to的时间不会自动变成>from时间,而是保持第一次选择的时候的过滤下拉菜单。请问怎么将to>from 变成动态能自动更新的呢?
还是我的更新的代码放错了位置呢?

谢谢了。

以下是选择”from“ date:


$(function() {
$('.from').datepicker(
{
dateFormat: "yy/mm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showOn: "button",
buttonImage: "../../static/calendar.gif",
buttonImageOnly: true,
//buttonText: "Select date",
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
$("#to").datepicker("option", "minDate", dateText);
}

if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');

$('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
}
},

beforeShow : function(input, inst) {

inst.dpDiv.addClass('month_year_datepicker')

if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});


选择to 时间的代码和from几乎一样,只有以下2行不同:


beforeShow : function(input, inst) {
var mindate = $('.from').datepicker('getDate'); //就是加了这两行
$(this).datepicker('option', 'minDate', mindate); //>就是加了这两行

inst.dpDiv.addClass('month_year_datepicker')


if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});

...全文
125 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
香蕉猪 2015-10-30
  • 打赏
  • 举报
回复
rdfzfanghui 2015-10-30
  • 打赏
  • 举报
回复
ok, 找到一个解决方案了: http://stackoverflow.com/questions/33407669/jquery-month-picker-mindate-doesnt-work

$( "#from, #to" ).datepicker({ 
           changeMonth: true,
           changeYear: true,
           showButtonPanel: true,
           dateFormat: 'MM yy',            
           onClose: function(dateText, inst) { 
               var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
               var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();             
               $(this).datepicker('setDate', new Date(year, month, 1));
           },
           beforeShow : function(input, inst) {
               if ((datestr = $(this).val()).length > 0) {
                   year = datestr.substring(datestr.length-4, datestr.length);
                   month = jQuery.inArray(datestr.substring(0, datestr.length-5), $(this).datepicker('option', 'monthNames'));
                   $(this).datepicker('option', 'defaultDate', new Date(year, month, 1));
                   $(this).datepicker('setDate', new Date(year, month, 1));    
               }
               var other = this.id == "from" ? "#to" : "#from";
               var option = this.id == "from" ? "maxDate" : "minDate";        
               if ((selectedDate = $(other).val()).length > 0) {
                   year = selectedDate.substring(selectedDate.length-4, selectedDate.length);
                   month = jQuery.inArray(selectedDate.substring(0, selectedDate.length-5), $(this).datepicker('option', 'monthNames'));
                   $(this).datepicker( "option", option, new Date(year, month, 1));
               }
           }
       });
       $("#btnShow").click(function(){ 
       if ($("#from").val().length == 0 || $("#to").val().length == 0){
           alert('All fields are required');
       }
       else{
           alert('Selected Month Range :'+ $("#from").val() + ' to ' + $("#to").val());
           }
       })

87,838

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧