Jquery ui jQueryUIDatePicker。禁用日期数组
我一直在努力寻找解决jQueryUIDatePicker问题的方法,但运气不好。这就是我想做的 我有一个应用程序,在这个应用程序中,我正在执行一些复杂的PHP操作,以返回一个JSON日期数组,我希望将其从jQueryUIDatePicker中屏蔽。我返回此数组:Jquery ui jQueryUIDatePicker。禁用日期数组,jquery-ui,jquery-ui-datepicker,Jquery Ui,Jquery Ui Datepicker,我一直在努力寻找解决jQueryUIDatePicker问题的方法,但运气不好。这就是我想做的 我有一个应用程序,在这个应用程序中,我正在执行一些复杂的PHP操作,以返回一个JSON日期数组,我希望将其从jQueryUIDatePicker中屏蔽。我返回此数组: ["2013-03-14","2013-03-15","2013-03-16"] 有没有一种简单的方法可以简单地说:从datepicker阻止这些日期 我已经阅读了UI文档,但没有看到任何对我有帮助的东西。有人有什么想法吗?你可以用它
["2013-03-14","2013-03-15","2013-03-16"]
有没有一种简单的方法可以简单地说:从datepicker阻止这些日期
我已经阅读了UI文档,但没有看到任何对我有帮助的东西。有人有什么想法吗?你可以用它来做
以下示例禁用日期2013年3月14日至2013年3月16日
var array = ["2013-03-14","2013-03-15","2013-03-16"]
$('input').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ array.indexOf(string) == -1 ]
}
});
演示:IE 8没有indexOf函数,所以我改用jQuery inArray
$('input').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [$.inArray(string, array) == -1];
}
});
如果还希望阻止星期日(或其他日期)以及日期数组,我将使用以下代码:
jQuery(function($){
var disabledDays = [
"27-4-2016", "25-12-2016", "26-12-2016",
"4-4-2017", "5-4-2017", "6-4-2017", "6-4-2016", "7-4-2017", "8-4-2017", "9-4-2017"
];
//replace these with the id's of your datepickers
$("#id-of-first-datepicker,#id-of-second-datepicker").datepicker({
beforeShowDay: function(date){
var day = date.getDay();
var string = jQuery.datepicker.formatDate('d-m-yy', date);
var isDisabled = ($.inArray(string, disabledDays) != -1);
//day != 0 disables all Sundays
return [day != 0 && !isDisabled];
}
});
});
在ShowDate之前不适合我,所以我继续开发自己的解决方案:
$('#embeded_calendar').datepicker({
minDate: date,
localToday:datePlusOne,
changeDate: true,
changeMonth: true,
changeYear: true,
yearRange: "-120:+1",
onSelect: function(selectedDateFormatted){
var selectedDate = $("#embeded_calendar").datepicker('getDate');
deactivateDates(selectedDate);
}
});
var excludedDates = [ "10-20-2017","10-21-2016", "11-21-2016"];
deactivateDates(new Date());
function deactivateDates(selectedDate){
setTimeout(function(){
var thisMonthExcludedDates = thisMonthDates(selectedDate);
thisMonthExcludedDates = getDaysfromDate(thisMonthExcludedDates);
var excludedTDs = page.find('td[data-handler="selectDay"]').filter(function(){
return $.inArray( $(this).text(), thisMonthExcludedDates) >= 0
});
excludedTDs.unbind('click').addClass('ui-datepicker-unselectable');
}, 10);
}
function thisMonthDates(date){
return $.grep( excludedDates, function( n){
var dateParts = n.split("-");
return dateParts[0] == date.getMonth() + 1 && dateParts[2] == date.getYear() + 1900;
});
}
function getDaysfromDate(datesArray){
return $.map( datesArray, function( n){
return n.split("-")[1];
});
}
对于DD-MM-YY,使用以下代码:
jQuery(function($){
var disabledDays = [
"27-4-2016", "25-12-2016", "26-12-2016",
"4-4-2017", "5-4-2017", "6-4-2017", "6-4-2016", "7-4-2017", "8-4-2017", "9-4-2017"
];
//replace these with the id's of your datepickers
$("#id-of-first-datepicker,#id-of-second-datepicker").datepicker({
beforeShowDay: function(date){
var day = date.getDay();
var string = jQuery.datepicker.formatDate('d-m-yy', date);
var isDisabled = ($.inArray(string, disabledDays) != -1);
//day != 0 disables all Sundays
return [day != 0 && !isDisabled];
}
});
});
变量数组=[“03-03-2017”,“03-10-2017”,“03-25-2017”]
$(“#日期选择器”)。日期选择器({
beforeShowDay:功能(日期){
var string=jQuery.datepicker.formatDate('dd-mm-yy',日期);
return[array.indexOf(string)=-1]
}
});
功能highlightDays(日期){
对于(变量i=0;i
如果您想禁用jquery datepicker中的特定日期,那么下面是一个简单的演示
<script type="text/javascript">
var arrDisabledDates = {};
arrDisabledDates[new Date("08/28/2017")] = new Date("08/28/2017");
arrDisabledDates[new Date("12/23/2017")] = new Date("12/23/2017");
$(".datepicker").datepicker({
dateFormat: "dd/mm/yy",
beforeShowDay: function (date) {
var day = date.getDay(),
bDisable = arrDisabledDates[date];
if (bDisable)
return [false, "", ""]
}
});
</script>
var arrDisabledDates={};
arrDisabledDates[新日期(“2017年8月28日”)]=新日期(“2017年8月28日”);
arrDisabledDates[新日期(“2017年12月23日”)]=新日期(“2017年12月23日”);
$(“.datepicker”).datepicker({
日期格式:“日/月/年”,
beforeShowDay:功能(日期){
var day=date.getDay(),
b禁用=arrDisabledDates[日期];
如果(b禁用)
返回[假,“,”]
}
});
有人能用Materialize datepicker提示我类似的问题吗?我有点明白你的意思了。我该如何将我的日期组合在一起。哇。你真是个天才,兄弟。我不知道为什么这个问题没有出现在堆栈溢出帖子中,因为它被问了一百万次。上面所有的例子都太复杂了,这很简单。5颗星。谢谢。我已经下载了代码,但是当我将上面的代码集成到其中时,它不起作用。请帮助一次投票是不够的。有些人把事情弄得比他们需要的更复杂。非常感谢您提供了一个简单有效的代码片段。非常有用。如果您还想排除这些自定义日期之外的周末,请将返回行替换为:returnarray.indexOf(string)!=-1.[错误]:$.datepicker.noWeekends(日期)
@ChristopherStrydom你会惊讶有多少人还在使用它。这是因为IE8是最新的IE浏览器,将在WinXP(SP3)上运行。但是它确实支持字符串上的indexOf()函数来查找该字符串中子字符串的位置。我们可以在dateRangePicker上做同样的事情吗?dateRangePicker是否可以,我想禁用日期范围选择器上的日期数组??您能定义您的问题吗?我有日期范围选择器假设:2017年1月1日-2017年3月31日。我想突出显示此范围之间的自定义日期数组中的一些日期。在日期范围日历上$日期=['2017年1月2日','2017年1月3日'…直到]这些日期。是否可能?与datepicker中一样,我们可以使用:beforeShowDay函数实现此目的,日期范围选择器中有什么:
<script type="text/javascript">
var arrDisabledDates = {};
arrDisabledDates[new Date("08/28/2017")] = new Date("08/28/2017");
arrDisabledDates[new Date("12/23/2017")] = new Date("12/23/2017");
$(".datepicker").datepicker({
dateFormat: "dd/mm/yy",
beforeShowDay: function (date) {
var day = date.getDay(),
bDisable = arrDisabledDates[date];
if (bDisable)
return [false, "", ""]
}
});
</script>