Javascript (JS)在一天中的某段时间内启用按钮
我一直试图在一天中的某段时间内启用一个按钮,但我无法使它工作 我一直想做的是: 按钮应在17:00到22:00(GMT+1)之间启用。 我在网站的帮助下编写了我的脚本,人们要求使用类似的系统,但我无法让它工作。。我希望你能帮助我 对不起我的英语^^ 以下是脚本:Javascript (JS)在一天中的某段时间内启用按钮,javascript,button,time,Javascript,Button,Time,我一直试图在一天中的某段时间内启用一个按钮,但我无法使它工作 我一直想做的是: 按钮应在17:00到22:00(GMT+1)之间启用。 我在网站的帮助下编写了我的脚本,人们要求使用类似的系统,但我无法让它工作。。我希望你能帮助我 对不起我的英语^^ 以下是脚本: <input class="submit" type="submit" id="checktime" value="Check"/> <script type="text/javascript" defer="def
<input class="submit" type="submit" id="checktime" value="Check"/>
<script type="text/javascript" defer="defer">
<!--
var enableDisable = function(){
var UTC_hours = new Date().getUTCHours() +1;
if ((UTC_hours == 17) && (UTC_hours == 18) && (UTC_hours == 19) && (UTC_hours == 20) && (UTC_hours == 21) && (UTC_hours == 22)){
document.getElementById('checktime').disabled = false;
else
document.getElementById('checktime').disabled = true;
}
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>
if语句不能像那样工作;你要测试的时间是17小时、18小时、19小时还是…,这显然是永远不会发生的,因为不可能同时出现所有这些时间。应该是:
if (UTC_hours > 16 && UTC_hours < 22)
if(UTC小时数>16和&UTC小时数<22)
这将确保至少是下午5点,但在晚上10点之前。您缺少括号,并且您的id错误,这应该可以工作
<input class="submit" type="submit" id="checktime" value="Check"/>
<script type="text/javascript" defer="defer">
<!--
var enableDisable = function(){
var UTC_hours = new Date().getUTCHours() +1;
if (UTC_hours > 16 && UTC_hours < 22){
document.getElementById('checktime').disabled = false;
}
else
{
document.getElementById('checktime').disabled = true;
}
};
setInterval(enableDisable, 1000*60);
enableDisable();
// -->
</script>
FWIW别忘了保护后端。@VisioN以前从未使用过javascript,所以我不知道你在说什么:P@user3174114javascript是一种客户端语言,这意味着用户可以随意更改/编辑它。如果您依赖于出于安全原因禁用按钮,请确保在服务器端验证时间(php、ruby、c#,无论您使用什么)。好的,我已经更改了If语句,但它似乎仍然无法正常工作。为了检查您的提示是否有效,我将disabled=false切换为disabled=true(反之亦然)。当我测试新的javascript时,按钮没有被禁用,但仍然处于活动状态。(这应该不是事实,因为我把假改为真)看到了。谢谢你的帮助;)意识到用身份证。。编辑了我的第一篇文章,但我想这有点太晚了:)我如何在GMT+5.30(印度)中使用这个?比如:Date().getUTCHours()+5.3?