【发布时间】:2015-02-01 22:23:55
【问题描述】:
我有四个数组,具有以下角色:
- 所有现有的开始日期(未选中)
- 所有现有的结束日期(未选中)
- 所选项目的开始日期(选中)
- 所选项目结束日期(选中)
1 和 2 包含集合中所有未选择事件的开始和结束时间戳池。
当与事件相关的复选框被选中时,3 和 4 将填充开始和结束时间戳,当它们被取消选中时,时间戳被删除。
结果应该是,如果未选中项的日期范围与新选中项的日期范围冲突,则阻止它们被选中并直观地显示它们是禁用的选项。
我目前将所有未选中项的值与比较数组中的最近检查日期进行比较,但它忽略了之前的值。这意味着我可以选择一个,冲突的日期被禁用,但是当我选择另一个可用的选项时,以前禁用的日期被重新启用。
我不是 100% 确定如何确保将所有未选中的项目与所有选中的项目进行比较,并根据日期是否冲突来禁用。在现有循环中添加嵌套 for 循环是解决此问题的最佳方法吗?
附言我知道有一些奇怪的选择器和额外的工作来格式化日期,但我这样做的人坚持认为日期格式是 MON ## - ##(同月)和 MON ## - MON ##(不同月)和日期包含在复选框标签中的一个小标签中。
var camp_dates;
var camp_start;
var camp_end;
var other_camp_dates;
var other_camp_start;
var other_camp_end;
var checked_start = [];
var checked_end = [];
var year = new Date().getFullYear();
// Change checkbox apply filters
$("#gform_11 input[type=checkbox]").click(function(){
//Reset list of checked items
checked_start = [];
checked_end = [];
camp_dates = $("label[for='" + $(this).attr("id") + "'] small").html().split("-");
camp_start = camp_dates[0].split(" ");
camp_end = camp_dates[1].split(" ");
//If no month in end date, assume same month as first (Ex. Jul 06-22 == Jul 06 - Jul 22)
if (camp_end.length == 1){
camp_end.unshift(camp_start[0]);
}
//rejoin day and months, add year to parse for timestamp
camp_start = Date.parse(camp_start.join(" ") + ", " + year);
camp_end = Date.parse(camp_end.join(" ") + ", " + year);
//Take empty start and arrays and add dates for selections
$(".gfield_checkbox input:checked").each(function(){
//All currently checked items
checked_start.push(camp_start);
checked_end.push(camp_end);
});
$(".gfield_checkbox input:not(:checked) + label small").each(function(){
//Gen values for all unselected items
other_camp_dates = $(this).html().split("-");
other_camp_start = other_camp_dates[0].split(" ");
other_camp_end = other_camp_dates[1].split(" ");
//If no month in end date, assume same month as first
if (other_camp_end.length == 1){
other_camp_end.unshift(other_camp_start[0]);
}
//rejoin day and months, add year to parse for timestamp
other_camp_start = Date.parse(other_camp_start.join(" ") + ", " + year);
other_camp_end = Date.parse(other_camp_end.join(" ") + ", " + year);
// Loop through arrays of start/end dates and compare to each unselected item - apply fade, disable, color
var i;
for (i = 0; i < checked_start.length; i++) {
if ( other_camp_start >= checked_start[i] && other_camp_start < checked_end[i] ||
other_camp_end > checked_start[i] && other_camp_end <= checked_end[i] ){
// If there is conflict
$(this).css("color", "red").parent().fadeTo("slow",0.5).siblings("input").not(":checked").attr("disabled", true);
} else {
$(this).css("color", "#7E7E7E").parent().fadeTo("slow",1).siblings("input").attr("disabled", false);
}
}
});
});
【问题讨论】:
-
查看您的 HTML 会有所帮助
-
更好的是,您认为您可以创建一个我们可以使用的JS fiddle demo 吗?示例日期和结构可以帮助我们更好地理解问题?
-
在原帖中包含一支笔,将其剥离到最低限度
-
顺便说一句 - 你应该真正满足跨新年的开始范围,例如 12 月 29 日至 1 月 1 日。可能不太可能......但可能。
-
是的,这听起来可能很混乱。如果由于年份换行而导致结束日期小于开始日期,那么在结束时间戳上简单地添加一年(31,536,000 秒)是否有意义?
标签: javascript jquery arrays loops for-loop