Итак, у меня есть средство выбора даты, которое позволяет выбирать только 2 даты в месяц, 1-ю и 15-ю. Проблема в том, что он по-прежнему позволяет выбирать предыдущие даты. Например, если это 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, ''];
},
});
2 ответа
$('#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-е, но сегодня 25 октября (после 1-го и 15-го), поэтому первой доступной датой для выбора будет 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">
Похожие вопросы
Новые вопросы
javascript
По вопросам программирования на ECMAScript (JavaScript/JS) и его различных диалектах/реализациях (кроме ActionScript). Обратите внимание, что JavaScript — это НЕ Java. Включите все теги, относящиеся к вашему вопросу: например, [node.js], [jQuery], [JSON], [ReactJS], [angular], [ember.js], [vue.js], [typescript], [стройный] и т. д.
.datepicker
не является функцией jQuery ...