日历:禁用选择特定日期

时间:2010-03-04 04:10:51

标签: javascript asp-classic datepicker

我一直想弄清楚如何解决我遇到的这个问题。我需要找出一种方法来检查日历上选择的日期,看看它周围的任何东西是不可点击的。换句话说,如果在所选日期之后禁用日期,则它不能再进一步。

以下是一个例子:

说它的3月和3月共有31天。 用户选择第9天。 因此,第1-8天是不可选择的(我已经编写了这部分)

现在说第10天是不可选择的,因为它已经被某种东西占据了。在这种情况下,将不允许用户点击超过9日的任何其他日期,因为它不能越过已经被某些东西占用的那一天。

我真的不知道如何检查,但我确实已经在已禁用的数组类型中已经过了几天。那个数组看起来像这样:

[10,15,20]

与上面的情况一样,我知道哪些日子不可选,但如果用户(再次,根据上面的示例)选择第11天并且跨越第10天已经无法选择,我不知道如何编码

以下是它的照片: http://img704.imageshack.us/img704/9803/cals.jpg

任何帮助和想法都会很棒!

谢谢, 大卫

好的,使用ASP我已经解决了大部分问题:

 sVal = tempResults 'This is the array of used dates already
 tmpDate = tmpDate + 1 'This is the date they chose (9) and added a 1 making it 10 (next day)
 theMonthDays = getDaysInMonth(theMonth, theYear) ' This just gets how many days are in the current selected month
 ArrayOfValues = Split(sVal, ",") 'this splits the array of used dates to be checked below

 For i = 0 To UBound(ArrayOfValues)     
    if tmpDate = CInt(ArrayOfValues(i)) then 'if it finds a date used already then block the rest
        dim z, theBlockedDays

        z = tmpDate
        do until z = theMonthDays + 1
            theBlockedDays = "'" & theMonth & "-" & z & "-" & theYear & "'," & theBlockedDays
            z = z + 1
        loop
        exit for
    end if
Next

现在适用于第9天但是如果我选择第11天则不起作用,因为第二天是打开的,然后是第二天打开。我该如何检查?

大卫

1 个答案:

答案 0 :(得分:1)

让我看看我是否清楚地理解你的问题。

假设阻止日期为d0, d1, d2, ...dn,按升序排列。

然后当用户选择一个不是阻止日期的日期k时,您:

  • 阻止日期[1..k](你说你已经完成了这部分)
  • 屏蔽日期[dj..31],其中j最低,dj > k

那么你如何找到dj?嗯,一般来说,这是二元搜索的设计问题。您已对d0, d1, ...dn进行了排序,因此二进制搜索会在dj中找到O(log n)。但是,由于我们讨论的是日历日期,因此只有31个可能的数字,因此线性搜索也不会有任何影响。

以下是一个简单的代码供您开始使用:

<script>

function search(v, a) {
  for (var i = 0; i < a.length; i++) {
    if (a[i] > v) {
      return a[i];
    }
  }
  return Infinity;
}

alert(search(5, [10, 15, 20]));  // 10 --> block out [10..31]
alert(search(12, [10, 15, 20])); // 15 --> block out [15..31]
alert(search(17, [10, 15, 20])); // 20 --> block out [20..31]
alert(search(25, [10, 15, 20])); // Infinity --> blockout [Infinity..31], i.e. nothing

</script>
相关问题