2015-03-02 27 views
0
<body> 
    <p id="datepairExample"> 
     <input type="text" id="date" class="date start" /> <input id="time" 
     type="text" class="time start" /> 
    </p> 
</body> 
<script type="text/javascript" src="js/jquery.min.js"></script> 
<script type="text/javascript" src="js/datepair.js"></script> 
<script type="text/javascript" src="js/bootstrap-datepicker.js"></script> 
<script type="text/javascript" src="js/jquery.datepair.js"></script> 
<script type="text/javascript" src="js/jquery.timepicker.js"></script> 
<script type="text/javascript" src="js/site.js"></script> 


<script> 
    $(document).ready(function() { 
     $('#datepairExample .time').timepicker({ 
     'showDuration' : true, 
     'timeFormat' : 'g:ia' 
    }); 

    $('#datepairExample .date').datepicker({ 
     startDate : "0d" 
    }); 
    $('#datepairExample .date').datepicker().on('changeDate', function(ev) { 
     var x = new Date(); 
     var s = document.getElementById("date").value + ""; 
     if (s.charAt(0) == '0') { 
      s = s.substring(1, s.length); 
     } 

     if (x.toLocaleDateString() == s) { 
      alert("abc"); 
      $('#datepairExample .time').timepicker({ 
       'disableTimeRanges' : [ [ '12am', x ] ] 
      }); 
     } 
     /* else{ 
      alert("pqr"); 
      $('#datepairExample .time').timepicker({ 
       'showDuration' : true, 
       'timeFormat' : 'g:ia' 
      }); 

     } */ 
    }); 

    //$("#datepairExample .date").datepicker("option", "startDate", -1); 
    /*$(function() { 
     $("#datepairExample .date").datepicker({ 
      numberOfMonths : 3, 
      showButtonPanel : true, 
      minDate : x 
     }); 
    }); */ 

    // initialize datepair 
    $('#datepairExample').datepair(); 

    }); 
    // initialize input widgets first 
    </script> 

禁用時間正常工作,如果在日期選擇器中只選擇當前日期並且在任何日期之後選擇當前日期時不起作用。在當天的時間選擇器中禁用時間無法正常工作

因此,基本上,只有當他們是第一件要選擇的東西時,纔會禁用時間,即使所選日期不是當前日期,日期也不會在禁用後啓用。

爲了通過再次選擇日期來檢查塊是否正在執行,我使用了'alert'語句,它們正在執行,但輸出沒有變化。預先感謝任何想要幫助我的人。

回答

0

試試這個

function getCurrentTime(date) { 
     var hours = date.getHours(), 
      minutes = date.getMinutes(), 
      ampm = hours >= 12 ? 'pm' : 'am'; 

     hours = hours % 12; 
     hours = hours ? hours : 12; // the hour '0' should be '12' 
     minutes = minutes < 10 ? '0'+minutes : minutes; 

     return hours + ':' + minutes + ' ' + ampm; 
    } 

    var timeOptions = { 
      'timeFormat': 'h:i A', 
      'disableTimeRanges': [['12am', getCurrentTime(new Date())]] 
     }; 

&您可以使用此

var timeOptions = { 
     'timeFormat': 'h:i A', 
     'minTime': getCurrentTime(new Date()) 
    };