gpt4 book ai didi

javascript - 是否可以在 jQuery UI Datepicker 中将选择下拉列表转换为水平月份列表?

转载 作者:行者123 更新时间:2023-11-30 09:31:49 25 4
gpt4 key购买 nike

我正在构建一个基于 jQuery UI Datepicker 的日历,可以选择一次显示 3 个月的日期范围。现在它看起来像屏幕截图: calendar

所以我需要在此日历上方添加一个 block ,其中包含从当前月份开始的 12 个水平放置的月份。我想我需要将带有月份的默认选择下拉列表转换为这个水平列表,但由于我对 JavaScript 的经验不足,我什至无法在互联网上类似示例的帮助下使用它。

months

当您在此 block 中选择月份时,它应该将日历切换到 3 个选定的月份。希望您能给我建议一个解决方案。

这是我当前的代码:

$("#datepicker").datepicker({
numberOfMonths: 3,
showButtonPanel: false,
dateFormat: 'dd.mm.yy',
minDate: 0,
maxDate: new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000),
beforeShowDay: function(date) {
var startDate = $.datepicker.parseDate('dd.mm.yy', $('#start-date').val());
var endDate = $.datepicker.parseDate('dd.mm.yy', $('#end-date').val());
if (startDate && endDate && startDate - endDate) {
if (endDate <= date && date <= startDate) {
if (startDate && date && (startDate.getTime() === date.getTime())) {
console.log('from end to start - backward - red-start ');
return [true, 'ui-red-end', ''];
}
if (endDate && date && (endDate.getTime() === date.getTime())) {
console.log('from end to start - backward - red-end ');
return [true, 'ui-red-start', ''];
}
console.log('from end to start - backward');
return [true, 'ui-state-selected-range', ''];
}
}
if (startDate <= date && date <= endDate) {
console.log('from start to end - forward');
if (startDate && date && (endDate.getTime() === date.getTime())) {
console.log('from start to end - forward - red-end');
return [true, 'ui-red-end', ''];
}
if (endDate && date && (startDate.getTime() === date.getTime())) {
console.log('from start to end - forward - red-start');
return [true, 'ui-red-start', ''];
}
}
if (startDate <= date && date <= endDate) {
console.log('from start to end');
return [true, 'ui-state-selected-range', ''];
}
return [true, '', ''];
},
onSelect: function(dateText, inst) {
var startDate = $.datepicker.parseDate('dd.mm.yy', $('#start-date').val());
var endDate = $.datepicker.parseDate('dd.mm.yy', $('#end-date').val());
if (!startDate || endDate) {
$('#start-date').val(dateText);
$('.start-date-visible').text(dateText);
$('#end-date').val('');
$('.end-date-visible').text('');
$(this).datepicker('option', dateText);
} else {
if (new Date(dateText) < startDate) {
var sDate = $('#start-date').val();
$('.start-date-visible').text(dateText);
$('#start-date').val(dateText);
$(this).datepicker('option', null);
$('.end-date-visible').text(sDate);
$('#end-date').val(sDate);

} else {
$('.end-date-visible').text(dateText);
$('#end-date').val(dateText);
$(this).datepicker('option', null);
}
}
}
});
// start date on default
$('#start-date').val($.datepicker.formatDate('dd.mm.yy', new Date()));
$('.start-date-visible').text($.datepicker.formatDate('dd.mm.yy', new Date()));
// end date on default
$('#end-date').val($.datepicker.formatDate('dd.mm.yy', new Date(new Date().getTime() +
6 * 24 * 60 * 60 * 1000)));
$('.end-date-visible').text($.datepicker.formatDate('dd.mm.yy', new Date(new Date().getTime() +
6 * 24 * 60 * 60 * 1000)));
td.ui-state-selected-range:first-child a {
border-radius: 20px 0 0 20px;
}
td.ui-state-selected-range:last-child a {
border-radius: 0 20px 20px 0;
}
.ui-red-start a {
position: relative;
background-color: #F29676;
border-radius: 20px;
border: 1px solid #f29676 !important;
}
.ui-red-start a:before {
content: '';
right: -1px;
left: 50%;
top: -1px;
bottom: -1px;
position: absolute;
border: 1px solid #f29676;
border-right: none;
background-color: #f8c3b1;
z-index: -1;
}
.ui-red-end a {
position: relative;
background-color: #F29676;
border-radius: 20px;
border: 1px solid #f29676 !important;
}
.ui-red-end a:before {
content: '';
left: -1px;
right: 50%;
top: -1px;
bottom: -1px;
position: absolute;
border: 1px solid #f29676;
border-left: none;
background-color: #f8c3b1;
z-index: -1;
}

.ui-state-selected-range .ui-state-default {
border: 1px solid #f29676 !important;
border-left: none !important;
border-right: none !important;
background: #f8c3b1 !important;
box-sizing: border-box;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<input type="text" id="start-date" style="visibility: hidden">
<input type="text" id="end-date" style="visibility: hidden">
<span class="start-date-visible"></span>
<span class="end-date-visible"></span>
<div id="datepicker"></div>

最佳答案

希望对你有帮助

使用destroy()在选择月份时在日期选择器上设置月份。

$("#datepicker").datepicker("destroy");

然后通过所选月份重新初始化日期选择器。

var today=new Date();
var newDate;
setCalender(today.getMonth());
var today=$("#datepicker").datepicker("getDate");
var todayMonth=today.getMonth();
setMonth(todayMonth);
clickMonth();
function setCalender(defaultMonth){
$("#datepicker").datepicker("destroy");
if(today.getMonth()==defaultMonth){
newDate=new Date(today.getFullYear(),today.getMonth(),today.getDate());
}
else if(today.getMonth()>defaultMonth){
newDate=new Date(today.getFullYear()+1,defaultMonth,today.getDate());
}
else{
newDate=new Date(today.getFullYear(),defaultMonth,today.getDate());
}
$("#datepicker").datepicker({
numberOfMonths: 3,
showButtonPanel: false,
dateFormat: 'dd.mm.yy',
defaultDate: newDate,
minDate: 0,
maxDate: new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000),
beforeShowDay: function(date) {
var dates=new Date();

var startDate = $.datepicker.parseDate('dd.mm.yy', $('#start-date').val());
var endDate = $.datepicker.parseDate('dd.mm.yy', $('#end-date').val());
if (startDate && endDate && startDate - endDate) {
if (endDate <= date && date <= startDate) {
if (startDate && date && (startDate.getTime() === date.getTime())) {
return [true, 'ui-red-end', ''];
}
if (endDate && date && (endDate.getTime() === date.getTime())) {
return [true, 'ui-red-start', ''];
}
return [true, 'ui-state-selected-range', ''];
}
}

if (startDate <= date && date <= endDate) {
if (startDate && date && (endDate.getTime() === date.getTime())) {
return [true, 'ui-red-end', ''];
}
if (endDate && date && (startDate.getTime() === date.getTime())) {
return [true, 'ui-red-start', ''];
}
}

if (startDate <= date && date <= endDate) {
return [true, 'ui-state-selected-range', ''];
}

return [true, '', ''];
},
onSelect: function(dateText, inst) {
var startDate = $.datepicker.parseDate('dd.mm.yy', $('#start-date').val());
var endDate = $.datepicker.parseDate('dd.mm.yy', $('#end-date').val());

if (!startDate || endDate) {
$('#start-date').val(dateText);
$('.start-date-visible').text(dateText);
$('#end-date').val('');
$('.end-date-visible').text('');
$(this).datepicker('option', dateText);
} else {
if (new Date(dateText) < startDate) {
var sDate = $('#start-date').val();
$('.start-date-visible').text(dateText);
$('#start-date').val(dateText);
$(this).datepicker('option', null);
$('.end-date-visible').text(sDate);
$('#end-date').val(sDate);

} else {
$('.end-date-visible').text(dateText);
$('#end-date').val(dateText);
$(this).datepicker('option', null);
}
}
}
});
// start date on default
$('#start-date').val($.datepicker.formatDate('dd.mm.yy', new Date()));
$('.start-date-visible').text($.datepicker.formatDate('dd.mm.yy', new Date()));
// end date on default
$('#end-date').val($.datepicker.formatDate('dd.mm.yy', new Date(new Date().getTime() +
6 * 24 * 60 * 60 * 1000)));
$('.end-date-visible').text($.datepicker.formatDate('dd.mm.yy', new Date(new Date().getTime() +
6 * 24 * 60 * 60 * 1000)));
}
function setMonth(todayMonth){
$(".months").empty();
var month=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var after=[];
var before=[];
$.each(month,function(i,item){
if(i>=todayMonth){
before.push("<label data-attr='"+i+"'>"+month[i]+"</label>");
}
else{
after.push("<label data-attr='"+i+"'>"+month[i]+"</label>");
}
});
$.each(before,function(i,item){
$(".months").append(before[i]);
});
$.each(after,function(i,item){
$(".months").append(after[i]);
});
for(var j=0;j<3;j++){
$(".months").find("label").eq(j).addClass("active");
}
}
function clickMonth(){
$(document).on("click","label",function(){
var index=$(this).attr("data-attr");
setMonth(index);
setCalender(index);
});
}
td.ui-state-selected-range:first-child a {
border-radius: 20px 0 0 20px;
}
td.ui-state-selected-range:last-child a {
border-radius: 0 20px 20px 0;
}
.ui-red-start a {
position: relative;
background-color: #F29676;
border-radius: 20px;
border: 1px solid #f29676 !important;
}
.ui-red-start a:before {
content: '';
right: -1px;
left: 50%;
top: -1px;
bottom: -1px;
position: absolute;
border: 1px solid #f29676;
border-right: none;
background-color: #f8c3b1;
z-index: -1;
}
.ui-red-end a {
position: relative;
background-color: #F29676;
border-radius: 20px;
border: 1px solid #f29676 !important;
}
.ui-red-end a:before {
content: '';
left: -1px;
right: 50%;
top: -1px;
bottom: -1px;
position: absolute;
border: 1px solid #f29676;
border-left: none;
background-color: #f8c3b1;
z-index: -1;
}

.ui-state-selected-range .ui-state-default {
border: 1px solid #f29676 !important;
border-left: none !important;
border-right: none !important;
background: #f8c3b1 !important;
box-sizing: border-box;
}
label.active{
font-weight:bold;
background-color: #2795ee;
}
.ui-datepicker .ui-datepicker-prev,.ui-datepicker .ui-datepicker-next{
display:none;
}
label.active:first-child {
border-top-left-radius: 50%;
border-bottom-left-radius: 50%;
}
label.active:nth-child(3n) {
border-top-right-radius: 50%;
border-bottom-right-radius: 50%;
}
.months>label {
display: inline-block;
padding: 10px;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<input type="text" id="start-date" style="visibility: hidden">
<input type="text" id="end-date" style="visibility: hidden">
<span class="start-date-visible"></span>
<span class="end-date-visible"></span>
<div class="months">
</div>
<div id="datepicker"></div>

关于javascript - 是否可以在 jQuery UI Datepicker 中将选择下拉列表转换为水平月份列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45782121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com