在月中阻止偶数天,天数为偶数 (JQuery UI)
Blocking even days in Months with even number of days (JQuery UI)
我必须制作日期选择器,它将像这样工作:如果所选月份有偶数天(30),则该月中的所有偶数天将被阻止选择(2,4,6...)等。如果所选月份的天数为奇数(31),则日期选择器中的所有奇数天将被阻止选择(1,3,5..)等。
我的问题是我不知道如何阻止偶数和奇数天,我只知道如何阻止整个月,而不是任何一天......免责声明:我知道已经发布了一些内容,例如如何阻止特定日期,但没有任何偶数或奇数天的内容。
这是我的代码:
<link rel="stylesheet" href="jquery-ui.min.css">
<script src="external/jquery/jquery.js"></script>
<script src="jquery-ui.min.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({beforeShowDay: nationalDays});
});
function nationalDays(date){
var x = date.getMonth();
var r = x%2;
if(r == 0){
return [false];
}
else if(r == 1){
return [true];
}
}
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
在 beforeShowDay 选项中,您可以检查是否应该启用日期,并将数组中返回的第一项设置为 true
/false
。True 将启用该项目,而 false 将禁用它,如下所示:
$(function() {
$("#datepicker").datepicker({
defaultDate: new Date(),
beforeShowDay: function(date) {
var disabled = true, // date enabled by default
// get the number of days in current month
numOfDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
if (numOfDays % 2 == 0)
disabled = (date.getDate() % 2 != 0) // for even-days months, disable the even dates
else disabled = (date.getDate() % 2 == 0) //for odd - days months, disable the odd dates
return [disabled, ""]
}
});
});
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker" />
</p>
相关文章:
- jQuery UI自动完成突然停止工作
- Rails/JSON:如何将JSON用于jquery UI自动完成表单
- 停止jQuery UI滑块移动超过给定值
- Jquery UI自动完成无法工作
- jquery ui滑块上的滑块值
- jQuery UI可排序-多连接列表拖动
- 使用Jquery ui时滑块无法工作
- JQuery UI可拖动潜水与滚动棒到鼠标
- jQuery UI自动完成-修改问题
- JQuery UI日期选择器:在选择出发日期时自动弹出返回日期
- 阻止选项卡缓存jquery ui
- 将jQuery UI Timepicker Addon与React一起使用
- 如何在页面重新加载时显示jquery ui对话框
- jquery ui自动完成导致标头错误
- jquery UI draggable:UI.children不是一个函数
- 在JQuery UI Accordion Sortable中使用touchpunch无法正确使用touch
- Jquery UI对话框不会消失
- jQuery UI对话框错误,按钮导致HierarchyRequestError
- jQuery UI使用json文件自动完成
- 如何从内部销毁jQuery UI小部件's原型