【问题标题】:jQuery DatePicker - selecting a whole weekjQuery DatePicker - 选择一整周
【发布时间】:2011-07-30 15:36:29
【问题描述】:

我正在使用两个 jQuery 日期选择器来选择日期范围。有没有办法选择一整周?这意味着在打开开始日期选择器时,会有两种选择方式:

  • 选择一个开始日期(不影响结束日期选择器)。
  • 选择一整周(这会将开始日期选择器设置为一周的第一天,然后将结束日期选择器设置为最后一天)。

在 asp 日历中,可以通过将SelectionMode 属性设置为“DayWeek”来完成类似的操作,但我没有办法在 jQuery datepicker 中实现这一点。可以吗?

【问题讨论】:

  • 你打算如何向用户表明有两种选择模式?
  • 在asp日历中有一个带有箭头的列供选择(见here),所以我想到了使用周数列。

标签: javascript jquery jquery-ui jquery-ui-datepicker


【解决方案1】:

这是我最终如何实现的:

// A click on a week number cell in the weeks column of the start datepicker
$("td.ui-datepicker-week-col").live("click", function()
{
    // Simulate a click on the first day of the week
    $(this).next().click();

    // Grab the date, and set the end of the week in the end datepicker
    var fromDate = $("#inputStartDate").datepicker("getDate");    
    var toDate = fromDate;
    toDate.setDate(fromDate.getDate() + 6);
    $("#inputEndDate").datepicker("setDate", toDate);
});

【讨论】:

    【解决方案2】:

    以下是两个输入字段都可以单击的示例:

        $(document).ready(function(){
            // date picker for Export part
            $(".datePicker").datepicker(
                    {
                        dateFormat: 'yymmdd',
                        showWeek: true,
                        firstDay: 1,
                        weekHeader: "",
                        showOtherMonths: true,
                        selectOtherMonths: true
                    }
            ).focus(function(){
                // prevent focus event firing twice
                var $this = $(this);
                var now = +new Date();
                var lastClicked = $this.data("lastClicked");
                if (lastClicked && (now - lastClicked) < 100) {
                    // Don't do anything
                    return;
                }
                $this.data("lastClicked", now);
    
                var el = this;
    
                // A click on a week number cell in the weeks column of the start datepicker
                $("td.ui-datepicker-week-col").click(function(){
                    // Simulate a click on the first day of the week
                    $(this).next().click();
    
                    var selectedDate = $(el).datepicker("getDate");
                    $("#inputStartDate").datepicker("setDate", selectedDate);
                    var toDate = selectedDate;
                    toDate.setDate(toDate.getDate() + 6);
                    $("#inputEndDate").datepicker("setDate", toDate);
                });
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 2018-12-06
      • 1970-01-01
      相关资源
      最近更新 更多