【发布时间】:2017-02-02 15:40:44
【问题描述】:
我正在使用这个datepicker 和这个timepicker。我想开发 JS 代码来确定用户是否在 datepicker 上选择了当前日期,如果是这样,这将触发 timepicker 的一个函数,该函数将禁用实际时间之前的所有时间。我是 JavaScript 新手,所以我才到此为止。
HTML:
<input id="datemax" placeholder="Enter the date of incident" type="text"/>
<input id="timemax" placeholder="Enter the time of incident" type="text"/>
在当前时间之前禁用时间的功能,该功能有效但无论选择哪个日期都将保持不变:
function getCurrentTime(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
return hours + ':' + minutes + ' ' + ampm;
}
var timesAvailable = {
'timeFormat': 'h:i A',
'disableTimeRanges': [[getCurrentTime(new Date()), '12am']]
};
$('#timemax').timepicker(timesAvailable);
我想知道在我的表单中实现上述 JS 功能需要什么。我认为需要开发第二个函数,并且需要使用 var datemax = document.getElementById("datemax").value;从日期字段中调用值。
我这里开发了一些不完整的伪代码:
function datetimeSelect{
var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth();
var year = currentDate.getFullYear();
var datemax = document.getElementById("datemax").value;
if (datemax === currentDate){
//activate
'disableTimeRanges': [[getCurrentTime(new Date()), '12am']] //Can't select any time before now, greyed out
return false;
}
else //datemax does not equal to currentDate
{
$('#timeMax').timepicker();//Standard Datepicker and any time can be selected
return true;
{
因此,例如,如果选择了一个不是今天的日期,时间选择器将返回到它的默认功能,而没有任何灰色的时间。
【问题讨论】:
-
好吧,正如我之前指出的,你可以挂钩
changedatepicker 事件吗?我的意思是,如果当用户选择某个日期时,你可以运行一个函数,如果是,那么我可能有一个解决方案。我从未使用过那个插件之前所以当我去查看他们的文档时,我找不到任何事件 -
是的,这可能是一个相关的解决方案。请贴出来让我看看。
标签: javascript jquery datepicker timepicker