Select week number or date in JQuery UI datepicker [closed]

帅比萌擦擦* 提交于 2019-12-11 22:14:31

问题


This is the first time that I do something with jQuery UI and also my JS skills are at the beginner level.

What's my problem:

I wanna use JQuery UI datepicker to select:

  1. getting a single date when I click on a day or
  2. getting a date range when I click on the week number.

How can I get the date/date range? But it would be nice to get a simple working example.


回答1:


Maybe something like this would help you:

$( "#datepicker" ).datepicker({
showWeek: true,
firstDay: 1
});

// Highlight week on hover week number
$(document).on("mouseenter",".ui-datepicker-week-col",
               function(){$(this).siblings().find("a").addClass('ui-state-hover');} );
$(document).on("mouseleave",".ui-datepicker-week-col",
               function(){$(this).siblings().find("a").removeClass('ui-state-hover');} );

// Select week on click on week number
$(document).on("click",".ui-datepicker-week-col",
   function(){
       $first = $(this).siblings().find("a").first();
       $last = $(this).siblings().find("a").last();
       $first.click();
       $parentFirst = $first.parent();
       $parentLast = $last.parent();
       $("#datepicker").val(
       (Number($parentFirst.data("month"))+1)+"/"+$first.text()+"/"+$parentFirst.data("year")
           + " - " +
        (Number($parentLast.data("month"))+1)+"/"+$last.text()+"/"+$parentLast.data("year")
       );
    });

Fiddle demo: http://jsfiddle.net/lparcerisa/hw1tpthb/3/




回答2:


Just playing with the dates, subtracting days by 7 to get the weeks. Try this:

var selectedDate = $('#datepicker").datepicker( "getDate" );

var iterator = selectedDate;

var year = selectedDate.getFullYear();

var weeksCount = 0;
while (iterator.getFullYear() == year)
{
    iterator = new Date(iterator.getFullYear(),
                        iterator.getMonth(),
                        iterator.getDate()-7);

    weeksCount++;
}

weeksCount will have the week number :)



来源:https://stackoverflow.com/questions/25746358/select-week-number-or-date-in-jquery-ui-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!