我想在一天内多次隐藏提交按钮。
下面的代码有时可行,有时不起作用。昨天代码工作但今天代码不起作用。我无法理解问题所在。
<script>
window.addEventListener("load", function() {
// Check time and update the button's state every second.
setInterval(updateSubmitButtonState, 1000);
}, false);
function updateSubmitButtonState() {
var timezone = "Asia/Calcutta";
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var t =currentTime.getHours() + ":" + currentTime.getMinutes();
if (t >= '10:00' && t <= '10:20' ||
t >= '11:00' && t <= '11:20' ||
t >= '13:00' && t <= '13:20' ||
t >= '15:15' && t <= '15:25' ||
t >= '17:30' && t <= '17:40' ||
t >= '19:00' && t <= '19:10' ||
t >= '20:30' && t <= '20:40' ) {
$("#submit").css("display", "none");
} else {
$("#submit").css("display", "block");
}
}
</script>
<input type="submit" id="submit" name="submit">
答案 0 :(得分:2)
这是因为您正在转换为字符串,并尝试按字典顺序排序。例如:17:07将被转换为“17:7”所以这将大于“17:30”。我建议使用此功能:
function timeToMinutes(h, m) {
return h * 60 + m;
}
然后你可以比较像
timeToMinutes(h, m) > timeToMinutes(10, 0) ...
试试这个示例代码
function timeToMinutes(h, m) {
return h * 60 + m;
}
var currentTime = new Date();
var mins = timeToMinutes(currentTime.getHours(), currentTime.getMinutes());
alert(mins >= timeToMinutes(19, 20) && mins <= timeToMinutes(19, 30));
答案 1 :(得分:2)
您正在使用字符串进行检查,我建议您将时间更改为毫秒。
试一试。
<script>
window.addEventListener("load", function() {
// Check time and update the button's state every second.
setInterval(updateSubmitButtonState, 1000);
}, false);
function convertMilliSec(hr, min){
return (parseInt(hr) * (60000 * 60)) + (parseInt(min) * 60000)
}
function updateSubmitButtonState() {
var timezone = "Asia/Calcutta";
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var t = convertMilliSec(currentTime.getHours(), currentTime.getMinutes());
console.log(t);
if (t >= convertMilliSec(10,00) && t <= convertMilliSec(10,20) ||
t >= convertMilliSec(11,00) && t <= convertMilliSec(11,20) ||
t >= convertMilliSec(14,00) && t <= convertMilliSec(14,34) ||
t >= convertMilliSec(13,00) && t <= convertMilliSec(13,20) ||
t >= convertMilliSec(15,00) && t <= convertMilliSec(15,20) ||
t >= convertMilliSec(17,00) && t <= convertMilliSec(17,20) ||
t >= convertMilliSec(19,00) && t <= convertMilliSec(19,20) ||
t >= convertMilliSec(20,00) && t <= convertMilliSec(20,20) ) {
$("#submit").css("display", "none");
} else {
$("#submit").css("display", "block");
}
}
</script>
答案 2 :(得分:1)
您需要将hours
和minutes
转换为两位数字(零线索),如下所示:
var hours = currentTime.getHours();
hours = hours > 9 ? hours : '0'+hours.toString();
var minutes = currentTime.getMinutes();
minutes= minutes > 9 ? minutes: '0'+minutes.toString();
var t = hours + ":" + minutes;
或者您需要将所有这些转换为Date
对象:
var t = currentTime.getHours() + ":" + currentTime.getMinutes();
var dObj = new Date('01/01/2017 '+t+':00');
然后在比较时还要与Date
对象进行比较:
if(dObj >= new Date('01/01/2017 10:00:00') && t <= new Date('01/01/2017 10:20:00') .....
<强>更新强>
这是第二种方法的JSFiddle:
https://jsfiddle.net/02z5hbo1/2/