在特定日期和时间启用按钮时间 [英] Enable Button during certain day & time
问题描述
我试图每天仅在下午5点到10点之间启用按钮,星期一除外.禁用该按钮后,应显示< p></p>
(如通知访问者为何禁用该按钮).
I am trying to enable a button ONLY during 5PM to 10PM every day, except Monday.
When the button is disabled, <p></p>
should show up (like a notification to the visitor why it is disabled.)
我确实尝试过自己编写JavaScript,但似乎无法正常工作.我对这种语言一无所知,脚本是在其他网站的帮助下完成的.
I did try to write the JavaScript on my own, but it seem not to work correctly. I don't know anything about that language and did the script with aid of different sites.
这是我的剧本:
<input class="submit" type="submit" id="checktimer" value="Check"/>
<p id=timer style="display: none;">Lorem ipsum</p>
<script type="text/javascript" defer="defer">
<!--
var enableDisable = function(){
var UTC_hours = new Date().getUTCHours() +1;
var day = new Date().getDay();
if (day == 1){
document.getElementById('checktimer').disabled = true;
document.getElementById('timer').style.display = 'block';
}
else{
if (UTC_hours > 16 && UTC_hours < 22){
document.getElementById('checktimer').disabled = false;
document.getElementById('timer').style.display = 'none';
}
else
{
document.getElementById('checktimer').disabled = true;
document.getElementById('timer').style.display = 'block';
}
}
};
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>
推荐答案
尝试在元素上设置属性,而不是在元素对象上设置属性:
Try setting the attribute on the element, instead of a property on the element object:
document.getElementById('checktimer').setAttribute('disabled');
要删除它,请使用
document.getElementById('checktimer').removeAttribute('disabled');
就像其他人提到的那样,您应该将 checktimer
元素缓存在变量中,而不是每次都查找它.
As others have mentioned, you should cache the checktimer
element in a variable, instead of looking it up each time.
我更改了其他几项小事:
A couple of other minor things I changed:
- 删除了您拥有的Javascript评论内容.您不需要那些.
- 为
p
元素的id
属性值添加引号.
- Removed those Javascript comment things you had. You don't need those.
- Added quotes around the value of the
id
attribute for yourp
element.
这篇关于在特定日期和时间启用按钮时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!