2017-03-25 67 views
0

日期選擇器未來日期不會禁用。這裏是HTML如何使用jquery在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> 

回答

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> 
+0

的maxDate:0不工作 –

+0

引號,如下:'{maxDate:'0'}' – hassan

+0

我的意思是我完全使用你的代碼,但它不工作。 –

1

$(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); 
 

 
    
 
});
<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>

+0

我不知道爲什麼它不適用於我的,我的datepicker btw是從bootstrap。 –

+0

在你的機器上運行我的代碼並測試它是否工作 –

+0

如果在JavaScript文件中出現錯誤,那麼所有的功能將無法正常工作,或者如果你添加多個JavaScript源文件,那麼它將無法正常工作 –

相關問題