因此我可以向日期选择器添加自定义按钮。我想要添加的是季度按钮,即 Q1、Q2、Q3 和 Q4。
我不明白的是如何在按下其中一个按钮时设置特定日期。
例如当按下 Q1 时,会将日期设置为 3 月 31 日。 现在我只是将其设置为当前年份,我会添加年份下拉列表或稍后的内容。
我尝试了以下方法,但不起作用:
$( "<button>", {
text: "Q4",
click: function(event)
{
var inst = $.datepicker._curInst;
var newDate = new Date();
inst.selectedDay = inst.currentDay = newDate.getDate();
inst.drawMonth = inst.selectedMonth = inst.currentMonth = newDate.getMonth();
inst.drawYear = inst.selectedYear = inst.currentYear = newDate.getFullYear();
//Code to clear your date field (text box, read only field etc.) I had to remove the line below and add custom code here
$.datepicker._selectDate(event.target, $.datepicker._formatDate(inst, inst.currentDay, inst.currentMonth, inst.currentYear));
}
}).appendTo( quarterContainer )
在这个例子中,我只是设置当前日期,但是当我开始工作时,我将传递一个日期。
最佳答案
我认为这应该可行,但很容易通过谷歌搜索:)
$('#Q1').click(function(){
$('#datepicker').datepicker("setDate", new Date(2018,31,03) );
})
关于Javascript - 使用自定义按钮将日期选择器设置为特定日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49632899/