$days_off,"dates"=>$result-6ren">
gpt4 book ai didi

php - jquery datepicker 动态禁用特定日期和日期

转载 作者:行者123 更新时间:2023-12-01 05:39:29 26 4
gpt4 key购买 nike

我想从 MySQL 数据库记录中动态阻止特定日期和工作日。我已经做到了一定程度,无法继续进行。

json_encode(array("days"=>$days_off,"dates"=>$resultarr))

上面将输出以下内容:

{"days":["0","1","1","1","1","1","0"],"dates":["2015-06-01","2015-06-02","2015-06-03","2015-06-04","2015-06-05"
,"2015-06-06","2015-07-09","2015-07-10","2015-07-28","2015-07-29","2015-07-30","2015-07-31"]}

而在前端,jquery是这样写的:

success: function(data){
var dateToday = new Date();
var ndata = $.parseJSON(data);
if ($('#datepicker').length) $("#datepicker").datepicker({
minDate: dateToday, firstDay: 1, //endDate: '+0m',
dateFormat: "DD d MM yy", showOtherMonths:false,
beforeShowDay: function(date){
var day = date.getDay();
var selected = $("#datepicker").datepicker('getDate');
return [ndata[day] !=0, ""];
}
});
}

上面的代码只会阻止天数(周一至周日),但无法阻止我从上面的 json 数据中获取的特定日期。

最佳答案

您需要检查该日期是否与 JSON 中传递的任何日期相同。可以这样做:

beforeShowDay: function(date) { 

//Check if it's a forbidden day of the week.
var day = date.getDay();
var okDay = (ndata.days[day] != 0);

//Check if it's a forbidden date.
var formatted_date = $.datepicker.formatDate('yy-mm-dd', date)
var okDate = ($.inArray(formatted_date, ndata.dates) == -1);

//Only return true if both day of week and date are allowed.
return [okDay && okDate]

}

请注意,我尚未测试此代码。另外,我从 ndata[day] 更改为 ndata.days[day] 因为这似乎更符合您的 JSON,但我可能是错的。 Here是关于 .inArray() 的文档,以及 here是关于datepicker.formatDate()的文档。

关于php - jquery datepicker 动态禁用特定日期和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31647576/

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