繁体   English   中英

如何在截止日期后禁用 javascript 中的提交按钮

[英]How do i disable submit button in javascript after deadline

如何在截止日期后禁用 JavaScript 中的提交按钮。

请我需要提交一个按钮,以便在截止日期后带走,或者返回一个截止日期已过的警报。

谢谢

 <body> <h2>Welcome: </h2> <p>Please find below your responsibilities for the month</p> <div class=t ask> Task 1 </div> <p>Please Submit your report in the space below</p> <h3 style="color: red"> DEADLINE: 20/04/2020</h3> <form> <textarea> </textarea> <br> <br> <button>Submit</button> </form> </body>

您可以使用windows.setTimeout调用来触发事件以隐藏提交按钮。 要计算此事件在未来应该发生的毫秒数,您可以创建一个Date为 2020 年 4 月 20 日的日期4/20/2020和一个当前日期的第二个Date object。 然后对这两个对象调用getTime() 该函数定义为:

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.

这两个值之间的差异是未来4/20/2020 12:00:00 AM发生的毫秒数,应该是调用window.setTimeout的参数。 对于以下演示,在完成此计算后,计算值将被 10,000 毫秒覆盖。 用于演示目的:

 document.addEventListener('DOMContentLoaded', function(event) { let deadline = new Date(2020, 4, 20); // at 12:00 AM let now = new Date(); // current date and time let milliseconds = deadline.getTime() - now.getTime(); milliseconds = 10000; // set to 10 seconds for testing purposes let submit = document.getElementById('submit'); if (milliseconds > 0) { window.setTimeout(function() { submit.style.display = 'none'; }, milliseconds); } else { submit.style.display = 'none'; } });
 <body> <h2>Welcome: </h2> <p>Please find below your responsibilities for the month</p> <div class=t ask> Task 1 </div> <p>Please Submit your report in the space below</p> <h3 style="color: red"> DEADLINE: 20/04/2020</h3> <form> <textarea> </textarea> <br> <br> <button id="submit">Submit</button> </form> </body>

开发者发表的评论应该被认真对待。 您还应该在服务器端检查表单是否在 2020 年 4 月4/20/2020 12:00:00 AM或之后使用适当的时区提交。 在客户端,我们使用的是本地时区,但两个日期之间的毫秒数差异应该在某种程度上与时区无关,尽管可能存在细微差异。

如果您想精确计算到期的毫秒数,您可以使用:

let deadline = new Date(2020, 4, 20).toLocaleString("en-US", {timeZone: "America/New_York"});
deadline = new Date(deadline);
let now = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
now = new Date(now);

当然,您可以使用适合您的应用程序的任何时区。 请注意,在分配deadline = new Date(2020, 4, 20).toLocaleString("en-US", {timeZone: "America/New_York"}); 你最终会得到一个没有getTime function 的Date object ,因此需要从中构造一个新的Date object 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM