jQuery UI Datepicker 条件混乱..有没有更干净的方法来做到这一点?
我正在使用 jQuery UI Datepicker 设置一些“约会”调度程序,但我有我需要实现的一些条件:
- 客户无法在周日安排
- 客户无法在周六中午 12 点之后安排(尚未实现此功能)
- 如果当前时间已过,则无法安排下一天中午12点
- 不能提前大量安排(我选择+2个月)
- 无法安排当天(显然不是过去)
这是我当前的代码:
$(document).ready(function() {
function noSundays(date) {
return [date.getDay() != 0, ''];
}
var timezone = "EST";
$.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
function(data){
if (data.hour < 12) {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+1d',
maxDate: '+2m' });
} else {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+2d',
maxDate: '+2m' });
}
})
});
所以我们设置 noSundays,从 JSON 服务器获取 EST 中的当前时间,如果是在中午 12 点之前,我们使用 minDate : +1d (明天)启动 .datepicker,如果在中午 12 点之后,我们使用 minDate : +2d。这段代码可以工作,但我觉得我做错了什么。我还是比较缺乏经验,很想知道是否有更好的方法来写这个。
谢谢。
I'm setting up something of an "appointment" scheduler using the jQuery UI Datepicker, but I have a few conditions that I needed to implement:
- Customer could not schedule on a Sunday
- Customer can't schedule after 12pm on Saturday (have not implemented this)
- Can't schedule next day if the current time is past 12pm
- Can't schedule massively in advance (I chose +2 months)
- Can't schedule for the current day (and obviously not the past)
Here is my current code:
$(document).ready(function() {
function noSundays(date) {
return [date.getDay() != 0, ''];
}
var timezone = "EST";
$.getJSON("http://json-time.appspot.com/time.json?tz="+timezone+"&callback=?",
function(data){
if (data.hour < 12) {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+1d',
maxDate: '+2m' });
} else {
$("#datepicker").datepicker({
beforeShowDay: noSundays,
minDate: '+2d',
maxDate: '+2m' });
}
})
});
So we set noSundays, get the current time in EST from the JSON server and if it's before 12PM we fire up .datepicker with minDate : +1d (tomorrow), if it's after 12PM we use minDate : +2d. This code works but I feel like I'm doing something really wrong. I'm still relatively inexperienced and would love to know if there is a better way to write this.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以像这样更短
It can be shorter like this