时间不在下拉菜单中显示 – jQuery

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

Time Doesnt Populate in the Dropdown - Jquery

问题

以下是您提供的代码的翻译部分:

<html>
<head>
  <style>
    #booking-form {
      display: none;
    }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.0/themes/smoothness/jquery-ui.css">
  <script src="https://code.jquery.com/ui/1.13.0/jquery-ui.min.js"></script>
</head>
<body>
  <div id="calendar"></div>
  <div id="booking-form">
    <h2>预约</h2>
    <p>请选择日期和时间:</p>
    <form action="book-appointment.php" method="post">
      <input type="hidden" id="selected-date" name="selected-date" value="">
      <select id="selected-time" name="selected-time"></select>
      <br><br>
      <button type="submit">预约</button>
    </form>
  </div>
  <script>
    // 使用来自PHP的可用日期和时间
    var availableDates = ["2023-02-17","2023-02-19","2023-02-20","2023-02-25","2023-02-27"];
    var availableDateTime = {
      "2023-02-17":["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"],
      "2023-02-19":["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"],
      "2023-02-20":["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"],
      "2023-02-25":["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"],
      "2023-02-27":["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00"]
    };

    // 初始化日历
    $('#calendar').datepicker({
      minDate: 0, // 将minDate设置为0以禁用过去的日期
      beforeShowDay: function(date) {
        var dateString = $.datepicker.formatDate('yy-mm-dd', date);
        return [$.inArray(dateString, availableDates) != -1];
      },
      onSelect: function(dateText, inst) {
        // 在选择日期时显示预订表单
        $('#booking-form').show();

        // 存储所选日期
        $('#selected-date').val(dateText);

        // 清空时间选择中的先前选项
        $('#selected-time').empty();

        // 获取所选日期
        var selectedDate = new Date(dateText);

        // 将所选日期的可用时间添加到时间选择中
        var availableHoursForSelectedDate = availableDateTime[selectedDate.getFullYear() + '-' + (selectedDate.getMonth() + 1) + '-' + selectedDate.getDate()];

        $.each(availableHoursForSelectedDate, function(i, time) {
          var optionText = time;
          var optionValue = time;
          $('#selected-time').append($('<option>').text(optionText).val(optionValue));
        });

        // 在时间选择中禁用不可用的时间
        var currentDate = new Date();
        if (selectedDate.getDate() === currentDate.getDate()) {
          // 禁用当前小时之前的时间
          var currentHour = currentDate.getHours();
          $('#selected-time option').each(function() {
            var optionText = $(this).text();
            var hour = parseInt(optionText.substr(0, 2));
            if (hour < currentHour) {
              $(this).prop('disabled', true);
            }
          });
        }
        else {
          // 禁用上午9点之前和下午5点之后的所有时间
          $('#selected-time option').each(function() {
            var optionText = $(this).text();
            var startHour = parseInt(optionText.substr(0, 2));
            var endHour = parseInt(optionText.substr(8, 2));
            if (startHour < 9 || endHour > 16) {
              $(this).prop('disabled', true);
            }
          });
        }
      }
    });
  </script>
</body>
</html>

希望这能帮助您理解代码的翻译。如有其他问题,请随时提出。

英文:

I have the following code, which works well but the problem is when I select the date it doesn't populate the selected-time dropdown, its blank. It should populate the correct available hours for the selected date in that dropdown

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

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

&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
#booking-form {
display: none;
}
&lt;/style&gt;
&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://code.jquery.com/ui/1.13.0/themes/smoothness/jquery-ui.css&quot;&gt;
&lt;script src=&quot;https://code.jquery.com/ui/1.13.0/jquery-ui.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;calendar&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;booking-form&quot;&gt;
&lt;h2&gt;Book an appointment&lt;/h2&gt;
&lt;p&gt;Please select a date and time:&lt;/p&gt;
&lt;form action=&quot;book-appointment.php&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;hidden&quot; id=&quot;selected-date&quot; name=&quot;selected-date&quot; value=&quot;&quot;&gt;
&lt;select id=&quot;selected-time&quot; name=&quot;selected-time&quot;&gt;&lt;/select&gt;
&lt;br&gt;&lt;br&gt;
&lt;button type=&quot;submit&quot;&gt;Book Appointment&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;script&gt;
// Use available dates and hours from PHP
var availableDates = [&quot;2023-02-17&quot;,&quot;2023-02-19&quot;,&quot;2023-02-20&quot;,&quot;2023-02-25&quot;,&quot;2023-02-27&quot;];
var availableDateTime = {&quot;2023-02-17&quot;:[&quot;09:00&quot;,&quot;10:00&quot;,&quot;11:00&quot;,&quot;12:00&quot;,&quot;13:00&quot;,&quot;14:00&quot;,&quot;15:00&quot;,&quot;16:00&quot;],&quot;2023-02-19&quot;:[&quot;09:00&quot;,&quot;10:00&quot;,&quot;11:00&quot;,&quot;12:00&quot;,&quot;13:00&quot;,&quot;14:00&quot;,&quot;15:00&quot;,&quot;16:00&quot;],&quot;2023-02-20&quot;:[&quot;09:00&quot;,&quot;10:00&quot;,&quot;11:00&quot;,&quot;12:00&quot;,&quot;13:00&quot;,&quot;14:00&quot;,&quot;15:00&quot;,&quot;16:00&quot;],&quot;2023-02-25&quot;:[&quot;09:00&quot;,&quot;10:00&quot;,&quot;11:00&quot;,&quot;12:00&quot;,&quot;13:00&quot;,&quot;14:00&quot;,&quot;15:00&quot;,&quot;16:00&quot;],&quot;2023-02-27&quot;:[&quot;09:00&quot;,&quot;10:00&quot;,&quot;11:00&quot;,&quot;12:00&quot;,&quot;13:00&quot;,&quot;14:00&quot;,&quot;15:00&quot;,&quot;16:00&quot;]};
// Initialize calendar
$(&#39;#calendar&#39;).datepicker({
minDate: 0, // Set minDate to 0 to disable past dates
beforeShowDay: function(date) {
var dateString = $.datepicker.formatDate(&#39;yy-mm-dd&#39;, date);
return [$.inArray(dateString, availableDates) != -1];
},
onSelect: function(dateText, inst) {
// Show booking form on date selection
$(&#39;#booking-form&#39;).show();
// Store selected date
$(&#39;#selected-date&#39;).val(dateText);
// Clear previous options from time select
$(&#39;#selected-time&#39;).empty();
// Get selected date
var selectedDate = new Date(dateText);
// Add available hours for the selected date to time select
var availableHoursForSelectedDate = availableDateTime[selectedDate.getFullYear() + &#39;-&#39; + (selectedDate.getMonth() + 1) + &#39;-&#39; + selectedDate.getDate()];
$.each(availableHoursForSelectedDate, function(i, time) {
var optionText = time;
var optionValue = time;
$(&#39;#selected-time&#39;).append($(&#39;&lt;option&gt;&#39;).text(optionText).val(optionValue));
});
// Disable unavailable hours in time select
var currentDate = new Date();
if (selectedDate.getDate() === currentDate.getDate()) {
// Disable hours before current hour
var currentHour = currentDate.getHours();
$(&#39;#selected-time option&#39;).each(function() {
var optionText = $(this).text();
var hour = parseInt(optionText.substr(0, 2));
if (hour &lt; currentHour) {
$(this).prop(&#39;disabled&#39;, true);
}
});
}
else {
// Disable all hours before 9am and after 5pm
$(&#39;#selected-time option&#39;).each(function() {
var optionText = $(this).text();
var startHour = parseInt(optionText.substr(0, 2));
var endHour = parseInt(optionText.substr(8, 2));
if (startHour &lt; 9 || endHour &gt; 16) {
$(this).prop(&#39;disabled&#39;, true);
}
});
}
}
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

代码部分不需要翻译,以下是已翻译的内容:

问题出在你构建用于查找availableDateTime对象的键上。具体来说,它没有包含月份和日期值的0填充。例如,今天是2023年2月18日,但selectedDate.getFullYear() + '-' + (selectedDate.getMonth() + 1) + '-' + selectedDate.getDate()将计算为2023-2-18,而不是2023-02-18

根据@Swati分享的链接,我们了解如何在格式化数字左侧填充'0',这是我们需要对格式化日期的月份和日期值都要做的事情。

代码变成了:

var dateKey = `${selectedDate.getFullYear()}-${('0' + (selectedDate.getMonth() + 1)).slice(-2)}-${('0' + selectedDate.getDate()).slice(-2)}`;

var availableHoursForSelectedDate = availableDateTime[dateKey];

这里是一个示例fiddle。

英文:

The issue is with the key you construct for your lookup on the availableDateTime object. Specifically, it doesn't include the 0 padding for the month and day values. For example, today is February 18, 2023; but selectedDate.getFullYear() + &#39;-&#39; + (selectedDate.getMonth() + 1) + &#39;-&#39; + selectedDate.getDate() will evaluate to 2023-2-18, not 2023-02-18.

Following the link that @Swati shared, we learn how to left pad our formatted digits with a &#39;0&#39;, which we would need to do for both the month and day values of our formatted date.

The code becomes:

var dateKey = `${selectedDate.getFullYear()}-${(&#39;0&#39; + (selectedDate.getMonth() + 1)).slice(-2)}-${(&#39;0&#39; + selectedDate.getDate()).slice(-2)}`;
var availableHoursForSelectedDate = availableDateTime[dateKey];

Here is an example fiddle.

huangapple
  • 本文由 发表于 2023年2月18日 01:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487206.html
匿名

发表评论

匿名网友

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

确定