jQuery datepicker,自定义范围奇怪

编程入门 行业动态 更新时间:2024-10-13 18:27:26
本文介绍了jQuery datepicker,自定义范围奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用jQuery datepicker,并从MySQL数据库收集一组自定义的日期范围。来自查询的结果代码如下:

//<![CDATA [ $(window) .load(function(){ var ranges = [{start:new Date(2012,02,14),end:new Date(2012,03,31)}]; $(function ){ $(#datepicker)。datepicker({ numberOfMonths:1, dateFormat:'d MM,yy', beforeShowDay:function(date){ for(var i = 0; i< ranges.length; i ++){ if(date> = ranges [i] .start&& date< = ranges [i] .end) return [true,']; } return [false,'']; }, minDate:ranges [0] .start, maxDate:范围[ranges.length -1] .end }); }); }); //]]>

(查询的日期范围)。

我的问题是这样的:

当呈现日历时,由于某种原因,它决定将一个月添加到可选择的位置。也就是如果您将结束日期设置为3月31日,则将其更改为5月1日,或者将其设置为!st Feb,将允许您选择至3月1日。我已经尝试从日期减去1个月,但当然这并不适用,因为这几个月有不同的天数,所以如果我有3月31日在那里,我不能简单地减去1个月,因为第31 2月不存在。

任何想法,代码有什么问题?我很确定它的上面的jQuery代码!

你可以在 bookingtest.walesdesign

解决方案

我认为问题是你正在设置结束日期到4月31日,不存在(所以5月1日退货)。 请记住,从0(1月)到11(12月)的月份。这可以预期从2月14日到28日。

var ranges = [{start:new Date(2012,1,14),end:new Date(2012,2,28)}]; $(function(){ $(#datepicker)datepicker({ numberOfMonths:1, dateFormat:'d MM,yy', beforeShowDay:function(date){ for(var i = 0; i< ranges.length; i ++){ if(date> = ranges [i] .start&& = range [i] .end)return [true,'']; } return [false,'']; }, minDate:ranges [0]。 start, maxDate:ranges [ranges.length -1] .end }); });

I am using jQuery datepicker, with a custom set of date ranges collected from a MySQL db. The resulting code from the query looks like this:

//<![CDATA[ $(window).load(function(){ var ranges = [ { start: new Date(2012, 02, 14), end: new Date(2012, 03, 31) } ]; $(function() { $("#datepicker").datepicker({ numberOfMonths: 1, dateFormat: 'd MM, yy', beforeShowDay: function(date) { for(var i=0; i<ranges.length; i++) { if(date >= ranges[i].start && date <= ranges[i].end) return [true, '']; } return [false, '']; }, minDate: ranges[0].start, maxDate: ranges[ranges.length -1].end }); }); });//]]>

(It is the date ranges that are queried).

My problem is this:

When the calendar is rendered, for some reason it decides to add a month onto what is selectable. Ie. if you set the end date as 31st March, it will change it to 1st May, or if you set it as !st Feb, it will allow you to select upto 1st March. I have tried getting around this by subtracting 1 month from the date, but of course this does not work as the months have different numbers of days, so if I had the 31st March in there, I could not simply minus 1 month as the 31st Feb does not exist.

Any ideas what is wrong with the code? I am pretty sure its the jQuery code above!

You can see the example at bookingtest.walesdesign

解决方案

I think that the problem is that you are setting the end date to 31st of april, which doesn't exists (and so the 1st of May is returned). Remember that months go from 0 (January) to 11 (December). This works as expected from 14 February to 28 march

var ranges = [ { start: new Date(2012, 1, 14), end: new Date(2012, 2, 28) } ]; $(function() { $("#datepicker").datepicker({ numberOfMonths: 1, dateFormat: 'd MM, yy', beforeShowDay: function(date) { for(var i=0; i<ranges.length; i++) { if(date >= ranges[i].start && date <= ranges[i].end) return [true, '']; } return [false, '']; }, minDate: ranges[0].start, maxDate: ranges[ranges.length -1].end }); });

更多推荐

jQuery datepicker,自定义范围奇怪

本文发布于:2023-11-28 12:35:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   奇怪   jQuery   datepicker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!