You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery Datepicker的onSelect函数无法正常工作求助

解决方案
  • 先在页面中补充地点选择控件和配送时间展示容器,用于用户选择地点及展示对应可用时间:
<label class="label-size" for="location">Select Location&nbsp;<sup style="color:#e7377b"class="label-size">*</sup></label>
<select id="location" class="form-control" required>
  <option value="">Choose location</option>
  <option value="loc1">Location A</option>
  <option value="loc2">Location B</option>
</select>

<div id="delivery-time-container" class="mt-3" style="display:none;">
  <h5>Available Delivery Times</h5>
  <ul id="time-list"></ul>
</div>
  • 修改日期选择器的onSelect事件逻辑,移除调试用的alert,替换为获取日期、地点并渲染配送时间的代码:
$("#datefield1").datepicker({
  onSelect: function (date, datepicker) {
    if (date !== "") {
      const selectedLocation = $("#location").val();
      const timeContainer = $("#delivery-time-container");
      const timeList = $("#time-list");

      // 清空历史数据并隐藏容器
      timeList.empty();
      timeContainer.hide();

      // 校验地点是否已选择
      if (!selectedLocation) {
        alert("Please select a location first");
        return;
      }

      // 以下为核心逻辑:替换为实际后端API请求,传递date和location参数
      // 示例用模拟数据演示渲染逻辑
      const mockDeliveryTimes = [
        "9:00 AM - 11:00 AM",
        "1:00 PM - 3:00 PM",
        "4:00 PM - 6:00 PM"
      ];

      // 将时间列表渲染到页面
      mockDeliveryTimes.forEach(time => {
        timeList.append(`<li>${time}</li>`);
      });
      timeContainer.show();
    }
  }
});
  • 核心注意事项:
    1. 必须确保地点选择控件存在且能正确获取选中值,否则无法关联日期与地点查询配送时间
    2. 实际项目中需把模拟数据部分替换为真实后端接口请求,通过date和location参数获取对应可用时间
    3. 可根据需求调整时间展示样式,比如用按钮、卡片替代列表形式

内容的提问来源于stack exchange,提问作者Sachin Chawla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 16:25:22