2017-03-25 108 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源文件,那么它将无法正常工作 –

相关问题