jQuery datepicker- 2 个输入/文本框和限制范围
我使用带有两个输入框的 jQuery Datepicker 小部件,一个用于 "From" 日期,第二个用于 "To" 日期。我使用 jQuery Datepicker 功能演示 作为使两个输入框相互协作的基础,但是我需要能够添加这些额外的限制:
日期范围不能早于 2008 年 12 月 1 日
“截止”日期可以为否 晚于今天
“起始”日期一次 选择后,“截止”日期只能 在 7 天后的范围内 “从”日期
如果“结束”日期是 首先选择,然后选择“起始”日期 只能在7的范围内 “截止”日期之前的天数(带有 12 月 1 日为第一个限制 可选日期)
我似乎无法让上述所有内容一起工作。
总之,我希望能够选择 12 月 1 日到今天之间最多 7 天的范围(我意识到我是在 12 月 1 日发布此内容,所以暂时只能选择今天)。
到目前为止,我的代码
$(function () {
$('#txtStartDate, #txtEndDate').datepicker(
{
showOn: "both",
beforeShow: customRange,
dateFormat: "dd M yy",
firstDay: 1,
changeFirstDay: false
});
});
function customRange(input)
{
return {
minDate: (input.id == "txtStartDate" ? new Date(2008, 12 - 1, 1) : null),
minDate: (input.id == "txtEndDate" ? $("#txtStartDate").datepicker("getDate") : null),
maxDate: (input.id == "txtStartDate" ? $("#txtEndDate").datepicker("getDate") : null)
};
}
缺少 7 天范围限制,并且还阻止在 2008 年 12 月 1 日之前或今天之后选择“截止”日期。任何帮助将不胜感激,谢谢。
I am using the jQuery Datepicker widget with two input boxes, one for the "From" date and the second with the "To" date. I am using the jQuery Datepicker functional demo as a basis for getting the two input boxes to work with each other, but I need to be able to add these additional restrictions:
Date range can be no earlier than
01 December 2008"To" date can be no
later than todayOnce a "From" date
is selected, the "To" date can only
be within a range of 7 days after
the "From" dateIf a "To" date is
selected first, then the "From" date
can only be within the range of 7
days before the "To" date (with the
limit of 01 December being the first
selectable date)
I can't seem to get all of the above working together.
In summary, I would like to be able to select a range of up to 7 days between 01 December and today (I realise I am posting this on 1st December so will only get today for the moment).
My code so far
$(function () {
$('#txtStartDate, #txtEndDate').datepicker(
{
showOn: "both",
beforeShow: customRange,
dateFormat: "dd M yy",
firstDay: 1,
changeFirstDay: false
});
});
function customRange(input)
{
return {
minDate: (input.id == "txtStartDate" ? new Date(2008, 12 - 1, 1) : null),
minDate: (input.id == "txtEndDate" ? $("#txtStartDate").datepicker("getDate") : null),
maxDate: (input.id == "txtStartDate" ? $("#txtEndDate").datepicker("getDate") : null)
};
}
I'm missing the 7 day range restriction and also preventing a "To" date selection before 01 December 2008 or after today. Any help would be much appreciated, Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
非常感谢您的帮助,本,我在您的帖子的基础上提出了这个。现在已经完成并且运行得非常好!
这是一个工作演示。在 URL 中添加 /edit 以查看代码
完整代码如下 -
Many thanks for your help Ben, I have built upon your posts and have come up with this. It is now complete and works brilliantly!
Here's a Working Demo. Add /edit to the URL to see the code
Complete Code below-
我意识到我来晚了一点,但以下是我修改工作示例代码的方法。我不需要设置特定的最大和最小日期,只是不希望日期范围重叠,所以我只是让它们互相设置:(
我的日期选择器已经在脚本中进一步初始化,但这只是默认设置.)
似乎做了我需要做的事情:)
请参阅此处查看我的示例。
I realise I'm a little late to the party, but here is how I modified the working example code. I didn't need to set a specific maximum and minimum date, just didn't want a date range overlap, so I just let them set each other:
(My datepickers were already initialised in a script further up, but it's just default settings.)
Seems to do what I need it to :)
See here for my example.
好吧,这样怎么样:
这是我能想到的最好的,可以满足您的所有要求(我认为......)
Alright, how about this:
This is the best I could come up with that met all of your requirements (I think...)
考虑使用 rangeSelect 来拥有一个控件而不是两个。
为了实现您的目标,我想您需要添加一个 onSelect 侦听器,然后调用
datepicker( "option", settings )
来更改设置。Consider using rangeSelect to have one control instead of two.
To achieve what you are after, I suppose you need to add an onSelect listener and then call
datepicker( "option", settings )
to change the settings.您的 txtStartDate 开始日期不起作用,因为当它第二次检查 input.id 时,您的第二个 minDate 被设置为 null。另外,maxDate 应该检查 txtEndDate,而不是 txtStartDate。试试这个:
我不知道为什么用“+ 5”而不是“+ 7”,但如果我添加 0,我会得到我选择的日期加上下一天的可选日期范围。
Your start date for txtStartDate isn't working because your second minDate is being set to null when it checks the input.id the second time. Also, maxDate should be checking for txtEndDate, not txtStartDate. Try this:
I don't know why the '+ 5' instead of '+ 7', but if I add 0, I get a selectable date range of the day I picked plus the next.
这是我在大量挖掘我认为常见问题的解决方案后提出的解决方案。这有效地围绕兼容日期的共享输入范围“反弹”输入。含义 - 如果我有两个字段,其中一个可以用来约束另一个,另一个可以在必要时重新定义原始字段。这样做的目标是始终确保两个字段之间仅存在有限的天(或月或其他)范围。该特定示例还限制了未来可以在任一字段中选择某些内容的时间量(例如3个月)。
Here is a solution I came up with after a lot of digging for a solution to what I think is a common problem. This effectively 'bounces' the inputs around a shared input range of compatible days. Meaning - if I have two fields, either one can be used to constrain the other, and the other could redefine the original if necessary. The goal of this is to always ensure that there is only a finite range of days (or months or whatever) between the the two fields. This specific example also limits the amount of time into the future that something could be selected in either field (e.g. 3 months).
这就是我的使用方式:
this is how i use it:
我就是这样做的。我从 Jquery UI 网站获取了源代码并对其进行了修改以添加您的约束。
初步想法来自:http://jqueryui.com/demos/datepicker/#date-range
注意:您还需要一个选项来重置/清除日期(即,如果用户选择“起始日期”,则“截止日期”将受到限制。选择“起始日期”后如果用户现在选择“截止日期”,则“起始日期”也会受到限制。您需要有一个明确的选项来允许用户现在选择不同的“起始日期”。)
This is how I have done it. I have taken the source from the Jquery UI website and modified it to add your constraints.
Initial Idea from: http://jqueryui.com/demos/datepicker/#date-range
Note: You would also need an option to reset/clear the dates (i.e. if a user chooses a 'From date', the 'To date' becomes limited. After choosing a 'From date' if the user now chooses a 'To date', the From date also gets limited. You need to have a clear option to allow the user to choose a different "From" date now.)