如何使用jquery禁用datepicker中的未来日期?

时间:2017-03-25 13:36:40

标签: jquery html datepicker

Datepicker未来日期未禁用。这是我在HTML中的datepicker代码

<div class="input-group input-append date" id="dp3" data-date="" data-date-format="mm-dd-yyyy">
    <input class="form-control" style="width: 106px; height: 30px;" type="text" id="nodate" value="<?php echo date('m-j-Y');?>"  readonly=""/>
        <div class="input-group-addon add-on" style="width: 35px; height: 25px;"><i class="icon-calendar"></i></div>
        &emsp;
        <button type="button" class="btn btn-primary" onclick="window.location = '<?php echo base_url().'index.php/attendance/noRecordDate/';?>'+ $('#nodate').val()">Add Attendance</button>
</div>

这是我的剧本:

<script>
    $(function() {
    $( "#nodate" ).datepicker({  maxDate: new Date() });
});
</script>

2 个答案:

答案 0 :(得分:1)

  

maxDate

     

多种类型   支持:

Date: A date object containing the maximum date.
Number: A number of days from today. For example 2 represents two days from today and -1 represents yesterday.
String: A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs; valid periods are "y" for years, "m" for months, "w" for weeks, and "d" for days. For example, "+1m +7d" represents one month and seven days from today.

因此您需要正确设置日期对象,或者只需设置为字符串或数字,如下所示:

<script>
    $(function() {
    $( "#nodate" ).datepicker({  maxDate: '0' });
});
</script>

答案 1 :(得分:1)

&#13;
&#13;
$(document).ready(function () {

    var dbDate = "2017-03-24";
    var date2 = new Date(dbDate);

    $( "#nodate" ).datepicker({ 
    
      
     dateFormat: 'mm-dd-yy',
     maxDate: new Date()
    
    }).datepicker('setDate', date2);

    
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div class="input-group input-append date" id="dp3" data-date="" data-date-format="mm-dd-yyyy">
    <input class="form-control" style="width: 106px; height: 30px;" type="text" id="nodate" value=""  readonly=""/>
        <div class="input-group-addon add-on" style="width: 35px; height: 25px;"><i class="icon-calendar"></i></div>
        &emsp;
        <button type="button" class="btn btn-primary" onclick="window.location = '<?php echo base_url().'index.php/attendance/noRecordDate/';?>'+ $('#nodate').val()">Add Attendance</button>
</div>
&#13;
&#13;
&#13;