簡體   English   中英

Jquery 不允許在日期選擇器上選擇以前的日期

[英]Jquery don't allow previous dates to be selected on date picker

所以我有一個日期選擇器,它每月只允許選擇 2 個日期,第 1 個和第 15 個。 問題是它仍然允許選擇以前的日期。 例如,如果它是 10 月 1 日,則不應選擇所有以前的日期。 我怎樣才能使它不能選擇所有以前的日期?

<link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="side-datepicker">

$('#side-datepicker').datepicker({
     beforeShowDay: function (date) {
    //getDate() returns the day (0-31)
     if (date.getDate() == 1 || date.getDate() == 15) {
     return [true, ''];
    }
    return [false, ''];
    },


    });

 $('#side-datepicker').datepicker({ beforeShowDay: function (date) { let today = new Date('2019-10-24'); //if you want the current date to be selectable uncomment the following line //today.setDate(today.getDate() -1); if (date > today && (date.getDate() == 1 || date.getDate() == 15)) { return [true, '']; } return [false, '']; } });
 <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="side-datepicker">

您將要使用最小日期選項來強制日期選擇器的底部,如下所示:

 var currentTime = new Date() var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), +1); //first day of current month $('#side-datepicker').datepicker({ minDate: minDate, beforeShowDay: function (date) { //getDate() returns the day (0-31) if (date.getDate() == 1 || date.getDate() == 15) { return [true, '']; } return [false, '']; }, });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="side-datepicker">

如果您想將其限制為當前月份和尚未過去的日期。 示例僅允許 1 日和 15 日,但今天是 10 月 25 日(1 日和 15 日之后),因此第一個可供選擇的日期將是 11 月 1 日,然后您將修改您的最小日期,如下所示:

var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate()); //current date

前任:

 var currentTime = new Date() var minDate = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate()); //current date $('#side-datepicker').datepicker({ minDate: minDate, beforeShowDay: function (date) { //getDate() returns the day (0-31) if (date.getDate() == 1 || date.getDate() == 15) { return [true, '']; } return [false, '']; }, });
 <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="side-datepicker">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM