本文介绍了在jquery datepicker中启用一个特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的网站上有一个jQuery日期选择器,我已经禁用了本周的第一天和第二天(星期一和星期二)。此外,我有一系列禁用的日子(这是有效的)。现在我想启用24-12-2018,但我被困在这里。
I have a jQuery datepicker on my website, and I have disabled every first and second day (monday and tuesday) of the week. Also I have an array of days which are disabled (this is working). Now I would like to also enable 24-12-2018, but I am stuck here.
我的代码:
var vakantie = ["25-12-2018", "26-12-2018", "27-12-2018", "28-12-2018", "29-12-2018", "30-12-2018", "31-12-2018"];
function nietbeschikbaar(dt) {
var datestring = jQuery.datepicker.formatDate('dd-mm-yy', dt);
return [dt.getDay() == 1 || dt.getDay() == 2 ? false : true && vakantie.indexOf(datestring) == -1];
};
jQuery("#cfgen-element-7-6").datepicker(jQuery.datepicker.regional["nl"]);
jQuery("#cfgen-element-7-6").datepicker("option", "changeMonth", true);
jQuery("#cfgen-element-7-6").datepicker("option", "changeYear", true);
jQuery("#cfgen-element-7-6").datepicker("option", "minDate", +1);
jQuery("#cfgen-element-7-6").datepicker("option", "firstDay", 1);
jQuery("#cfgen-element-7-6").datepicker("option", "dateFormat", "dd-mm-yy");
jQuery("#cfgen-element-7-6").datepicker("option", "yearRange", "-0:+70");
jQuery("#cfgen-element-7-6").datepicker("option", "beforeShowDay", nietbeschikbaar);
我还设法让这段代码只能启用12月24日,我想已启用:
I also managed to get this code working to only enable the 24th of december, which I would like to have enabled:
var enableDays = ["24-12-2018"];
function enableAllTheseDays(date) {
var sdate = $.datepicker.formatDate('dd-mm-yy', date)
if ($.inArray(sdate, enableDays) != -1) {
return [true];
}
return [false];
}
jQuery("#datepicker").datepicker("option", "beforeShowDay", enableAllTheseDays);
如果可能的话,我想结合这些功能。所以要求:
I would like to combine these functions if this is possible. So the requirements:
- 每周的第1天和第2天禁用
- 'vakantie'阵列已停用
- 24-12-2018(12月24日)已启用
- Every 1st and 2nd day of the week disabled
- The 'vakantie' array disabled
- 24-12-2018 (24th of december) enabled
推荐答案
var vakantie = ["25-12-2018", "26-12-2018", "27-12-2018", "28-12-2018", "29-12-2018", "30-12-2018", "31-12-2018"];
var enableDays = ["24-12-2018"];
$("#datepicker").datepicker({beforeShowDay: function(dt) {
var datestring = $.datepicker.formatDate('dd-mm-yy', dt);
if($.inArray(datestring, enableDays) != -1) {
return [true];
}
else{
return [dt.getDay() == 1 || dt.getDay() == 2 ? false : true && vakantie.indexOf(datestring) == -1 ];
}
}});
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<p>Date: <input type="datepicker" id="datepicker"></p>
</body>
试试这个..
$("#datepicker").datepicker({beforeShowDay: function(dt) {
var datestring = $.datepicker.formatDate('dd-mm-yy', dt);
if($.inArray(datestring, enableDays) != -1) {
return [true];
}
else{
return [dt.getDay() == 1 || dt.getDay() == 2 ? false : true && vakantie.indexOf(datestring) == -1 ];
}
}});
这篇关于在jquery datepicker中启用一个特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!