提问人:Oluwafemi Akinyemi 提问时间:4/8/2020 最后编辑:j3ffOluwafemi Akinyemi 更新时间:4/8/2020 访问量:477
截止日期后如何在 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>
答:
0赞
Booboo
4/8/2020
#1
您可以使用调用来触发事件以隐藏提交按钮。若要计算未来发生此事件的毫秒数,请创建一个包含日期的对象,并使用当前日期创建另一个对象。然后调用这两个对象。此函数定义为:windows.setTimeout
Date
4/20/2020
Date
getTime()
Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
这两个值之间的差值是未来发生的毫秒数,并且应该是调用 的参数。对于以下演示,完成此计算后,计算值将覆盖 10,000 毫秒,以便进行演示:4/20/2020 12:00:00 AM
window.setTimeout
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>
开发人员发布的评论应认真对待。您还应该在服务器端进行检查,以检查表单是在使用适当的时区还是在使用适当的时区之后提交的。在客户端,我们使用的是本地时区,但两个日期之间的毫秒数差异应该在某种程度上与时区无关,尽管可能存在细微的差异。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"});
Date
getTime
Date
评论