选择HTML日历中的多个日期范围。

huangapple go评论60阅读模式
英文:

Jquery select multiple date ranges in HTML Calendar

问题

我使用一个HTML日历,可以选择多个日期范围。如何防止时间段重叠?

  • 第一次点击选择第一天

  • 第二次点击选择范围(必须在第一次点击之后)

  • 第三次点击选择下一个第一天

  • 第四次点击选择下一个范围

我的想法是通过index()来实现,但我不知道如何做...

我很高兴听到提示和帮助。我无法自行继续下去。

$("td.day").click(function() {

    if ($("td.firstClick").length == 0 && $(this).hasClass("reserved") == false) {
        $(this).addClass("firstClick");
    }
    
    if ($("td.firstClick").length > 0 && $(this).hasClass("firstClick") == false) {
        var tds = $("td.day");
        var firstClick = $(".firstClick");
        var firstClickIndex = tds.index(firstClick);
        var currentIndex = tds.index(this);
        
        tds.filter(function() {
            var idx = tds.index(this);
            return idx > firstClickIndex && idx < currentIndex;
            }).addClass("reserved");
    
        $(".firstClick").addClass("checkin").removeClass("firstClick");
        $(this).addClass("checkout");
    }
});
table {
  border-collapse: collapse;
}
table tr td {
  width: 14%;
}
table tr td:hover {
  cursor: pointer;
}
.firstClick {
  background: green;
}
.checkin {
  background: yellow;
}
.checkout {
  background: red;
}
.reserved {
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr>
    <td colspan="7"><b>2016</b></td>
  </tr>
  <tr>
    <td colspan="7"><i>November</i></td>
  </tr>
  <tr>
    <th>mon</th>
    <th>tue</th>
    <th>wed</th>
    <th>thu</th>
    <th>fri</th>
    <th>sat</th>
    <th>sun</th>
  </tr>
  <tr>
    <td></td>
    <td class="day">1</td>
    <td class="day">2</td>
    <td class="day">3</td>
    <td class="day">4</td>
    <td class="day">5</td>
    <td class="day">6</td>
  </tr>
  <!-- 这里省略了其他日期 -->
</table>
英文:

I use an HTML calendar where I can select multiple date ranges. How can I prevent time periods from overlap?

  • First click select first day

  • Second click select range (must be AFTER the first click day)

  • Third click select next first day

  • Fourth click select next range

My idea is to do it via the index(), but I don't know how....

I am glad about tips and help. I can't get any further on my own.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;td.day&quot;).click(function() {

    if ($(&quot;td.firstClick&quot;).length == 0 &amp;&amp; $(this).hasClass(&quot;reserved&quot;) == false) {
        $(this).addClass(&quot;firstClick&quot;);
    }
    
    if ($(&quot;td.firstClick&quot;).length &gt; 0 &amp;&amp; $(this).hasClass(&quot;firstClick&quot;) == false) {
        var tds = $(&quot;td.day&quot;);
        var firstClick = $(&quot;.firstClick&quot;);
        var firstClickIndex = tds.index(firstClick);
        var currentIndex = tds.index(this);
        
        tds.filter(function() {
            var idx = tds.index(this);
            return idx &gt; firstClickIndex &amp;&amp; idx &lt; currentIndex;
            }).addClass(&quot;reserved&quot;);
    
        $(&quot;.firstClick&quot;).addClass(&quot;checkin&quot;).removeClass(&quot;firstClick&quot;);
        $(this).addClass(&quot;checkout&quot;);
    }
});

<!-- language: lang-css -->

table {
  border-collapse: collapse;
}
table tr td {
  width: 14%;
}
table tr td:hover {
  cursor: pointer;
}
.firstClick {
  background: green;
}
.checkin {
  background: yellow;
}
.checkout {
  background: red;
}
.reserved {
  background: blue;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table border=&quot;1&quot;&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;7&quot;&gt;&lt;b&gt;2016&lt;/b&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;7&quot;&gt;&lt;i&gt;November&lt;/i&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;mon&lt;/th&gt;
    &lt;th&gt;tue&lt;/th&gt;
    &lt;th&gt;wed&lt;/th&gt;
    &lt;th&gt;thu&lt;/th&gt;
    &lt;th&gt;fri&lt;/th&gt;
    &lt;th&gt;sat&lt;/th&gt;
    &lt;th&gt;sun&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;1&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;2&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;3&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;4&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;5&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;6&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;7&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;8&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;9&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;10&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;11&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;12&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;13&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;14&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;15&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;16&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;17&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;18&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;19&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;20&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;21&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;22&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;23&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;24&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;25&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;26&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;27&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;28&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;29&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;30&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 0

在将reserved类添加到日期范围之前,请检查它们是否已被预订。

$("td.day").click(function() {

  if ($("td.firstClick").length == 0 && !$(this).is(".reserved, .checkin, .checkout")) {
    $(this).addClass("firstClick");
    return;
  }

  // 再次点击以切换第一个日期
  if ($(this).hasClass("firstClick")) {
    $(this).removeClass("firstClick");
    return;
  }

  if ($("td.firstClick").length > 0 && !$(this).hasClass("firstClick")) {
    var tds = $("td.day");
    var firstClick = $(".firstClick");
    var firstClickIndex = tds.index(firstClick);
    var currentIndex = tds.index(this);

    if (currentIndex < firstClickIndex) {
      alert("Checkin必须在Checkout之后");
      return;
    }

    var range = tds.filter(function() {
      var idx = tds.index(this);
      return idx > firstClickIndex && idx < currentIndex;
    });
    if (range.filter(".reserved, .checkin, .checkout").length > 0) {
      alert("无法选择重叠的范围");
      return;
    }
    range.addClass("reserved");
    $(".firstClick").addClass("checkin").removeClass("firstClick");
    $(this).addClass("checkout");
  }
});
table {
  border-collapse: collapse;
}

table tr td {
  width: 14%;
}

table tr td:hover {
  cursor: pointer;
}

.firstClick {
  background: green;
}

.checkin {
  background: yellow;
}

.checkout {
  background: red;
}

.reserved {
  background: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1">
  <tr>
    <td colspan="7"><b>2016</b></td>
  </tr>
  <tr>
    <td colspan="7"><i>November</i></td>
  </tr>
  <tr>
    <th>mon</th>
    <th>tue</th>
    <th>wed</th>
    <th>thu</th>
    <th>fri</th>
    <th>sat</th>
    <th>sun</th>
  </tr>
  <tr>
    <td></td>
    <td class="day">1</td>
    <td class="day">2</td>
    <td class="day">3</td>
    <td class="day">4</td>
    <td class="day">5</td>
    <td class="day">6</td>
  </tr>
  <tr>
    <td class="day">7</td>
    <td class="day">8</td>
    <td class="day">9</td>
    <td class="day">10</td>
    <td class="day">11</td>
    <td class="day">12</td>
    <td class="day">13</td>
  </tr>
  <tr>
    <td class="day">14</td>
    <td class="day">15</td>
    <td class="day">16</td>
    <td class="day">17</td>
    <td class="day">18</td>
    <td class="day">19</td>
    <td class="day">20</td>
  </tr>
  <tr>
    <td class="day">21</td>
    <td class="day">22</td>
    <td class="day">23</td>
    <td class="day">24</td>
    <td class="day">25</td>
    <td class="day">26</td>
    <td class="day">27</td>
  </tr>
  <tr>
    <td class="day">28</td>
    <td class="day">29</td>
    <td class="day">30</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>
英文:

Before you add the reserved class to the range of days, check if any of them are already reserved.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;td.day&quot;).click(function() {
if ($(&quot;td.firstClick&quot;).length == 0 &amp;&amp; !$(this).is(&quot;.reserved, .checkin, .checkout&quot;)) {
$(this).addClass(&quot;firstClick&quot;);
return;
}
// Click again to toggle first date
if ($(this).hasClass(&quot;firstClick&quot;)) {
$(this).removeClass(&quot;firstClick&quot;);
return;
}
if ($(&quot;td.firstClick&quot;).length &gt; 0 &amp;&amp; !$(this).hasClass(&quot;firstClick&quot;)) {
var tds = $(&quot;td.day&quot;);
var firstClick = $(&quot;.firstClick&quot;);
var firstClickIndex = tds.index(firstClick);
var currentIndex = tds.index(this);
if (currentIndex &lt; firstClickIndex) {
alert(&quot;Checkout has to be after checkin&quot;);
return;
}
var range = tds.filter(function() {
var idx = tds.index(this);
return idx &gt; firstClickIndex &amp;&amp; idx &lt; currentIndex;
});
if (range.filter(&quot;.reserved, .checkin, .checkout&quot;).length &gt; 0) {
alert(&quot;Can&#39;t select overlapping range&quot;);
return;
}
range.addClass(&quot;reserved&quot;);
$(&quot;.firstClick&quot;).addClass(&quot;checkin&quot;).removeClass(&quot;firstClick&quot;);
$(this).addClass(&quot;checkout&quot;);
}
});

<!-- language: lang-css -->

table {
  border-collapse: collapse;
}
table tr td {
  width: 14%;
}
table tr td:hover {
  cursor: pointer;
}
.firstClick {
  background: green;
}
.checkin {
  background: yellow;
}
.checkout {
  background: red;
}
.reserved {
  background: blue;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table border=&quot;1&quot;&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;7&quot;&gt;&lt;b&gt;2016&lt;/b&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td colspan=&quot;7&quot;&gt;&lt;i&gt;November&lt;/i&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;mon&lt;/th&gt;
    &lt;th&gt;tue&lt;/th&gt;
    &lt;th&gt;wed&lt;/th&gt;
    &lt;th&gt;thu&lt;/th&gt;
    &lt;th&gt;fri&lt;/th&gt;
    &lt;th&gt;sat&lt;/th&gt;
    &lt;th&gt;sun&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;1&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;2&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;3&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;4&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;5&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;6&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;7&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;8&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;9&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;10&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;11&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;12&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;13&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;14&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;15&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;16&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;17&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;18&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;19&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;20&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;21&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;22&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;23&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;24&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;25&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;26&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;27&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td class=&quot;day&quot;&gt;28&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;29&lt;/td&gt;
    &lt;td class=&quot;day&quot;&gt;30&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
    &lt;td&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 06:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定