一旦用户选择日期,我想从下拉列表中禁用过去的时间。 下面是示例代码:
function (declare, DateTextBox, locale, dom, lang, registry, ready) {
var pad, update_current_available_times, get_hour_string;
pad = function (n) {
n = n + '';
return n.length >= 2 ? n : new Array(2 - n.length + 1).join('0') + n;
},
get_hour_string = function (t) {
var hour = pad(t.getHours()-1);
var minute = pad(t.getMinutes());
return 'T' + hour + ':' + minute + ':00';
},
在几分钟内,我进行了如下更改,但给出了错误的结果。但是当尝试从分钟中禁用过去的时间时,给出了意想不到的结果。
get_hour_string = function (t) {
var hour = pad(t.getHours()-1); //added -1 to disable past time when used dojo1.9
var d1 = new Date (),
d2 = new Date ( d1 );
d2.setMinutes ( d1.getMinutes() + 20 );
var minute1 = pad(d2.getMinutes());
alert("d2.getMinutes() : " + d2.getMinutes() );
return 'T' + hour + ':' + minute1 + ':00';
}
请建议需要对 javascript 文件进行哪些更改才能修复禁用分钟下拉列表中的过去时间的问题。
最佳答案
在我看来,你已经快要工作了..?
如果我看的是这个,在你的小时选择器的 onChange 函数中,你只需要更改:
useMin = 'T02:' + pad(now.getMinutes()) + ':00';
到
useMin = 'T' + pad(v.getHours()) + ':' + pad(now.getMinutes()) + ':00';
这个有用吗? http://jsfiddle.net/8o23tbLu/27/
编辑:啊,我看到您使用了一个小技巧,只在分钟下拉列表中显示分钟。您可能可以只使用 dijit/form/Select 来代替,但这是一个很好的技巧!
由于您已将visibleIncrement和clickableIncrement设置为02:05:00和02:00:00,因此下拉列表实际显示的是:
00:00:00
02:05:00
04:10:00
...
22:55:00
因此,在设置分钟下拉列表的最小约束时,您实际上还必须设置“不可见”小时。
举个例子,如果“现在”是 14:34:00,则您必须使用 34 分钟,检查它属于哪个 2 小时“组”: 34 % 5 = 6 左右
。
然后从中获取小时:2 * 6 = 12
.. 因此,在这种情况下,分钟下拉列表的 useMin
将为 T12:34:00。
useMin = 'T' +
pad(2 * Math.floor(now.getMinutes() / 5)) + ':' +
pad(now.getMinutes()) + ':00';
至少我认为这样就可以了:)可能有一个更简单的解决方案。
关于javascript - 禁用过去的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26383752/