1

我正在使用这段代码:

<script type="text/javascript">      
  $(function() {

   var picker= $('#timepicker').datetimepicker({
      pickDate: false,
      pickSeconds: false,
       minView: 2,
      pick12HourFormat: true,
       startDate: new Date(<?php echo date('Y,m,d,h,i'); ?>),
      // endDate: '<?php echo date('Y-m-d h:i'); ?>'
    }).on('changeDate', function(ev){
        var startDate = new Date();
        //var df = get_time_difference(ev.localDate,startDate);
         // alert(startDate.getHours());
         // alert(ev.localDate.getHours());
         if(startDate.getHours() > ev.localDate.getHours()){
            // ev.stopPropagation();
            // ev.stopImmediatePropagation();
            // ev.preventDefault();
            $("#time").val('Error');
             //$('#time').unbind('changeDate');
            return false;
          }
      });
  });
</script>

当我选择低于当前时间的时间时,它应该显示错误,但关闭后,它显示选定的时间,

我想阻止事件更改输入字段#time 上的日期。

问候,

4

1 回答 1

2

The docs on http://tarruda.github.io/bootstrap-datetimepicker/ tells you: The only event exposed is ‘changeDate’. But when you close the date/time popup selector 'hide' is also triggerd. You can use this trigger to do you check again and (re)set $("#time") on error. You need to read '$("#time").val()' in the hide event to get the new datetime entered. Use new Date($("#time").val()) to covert it to a date and make it possible to apply getHours().

Try:

 $('#timepicker').datetimepicker().on('hide',function(e){ 
    if(new Date($("#time").val()).getHours()<12) 
                   {$("#time").val('Also error');}
 });
于 2013-05-18T11:03:04.810 回答