简体   繁体   English

如何禁用提交按钮一周

[英]How to disable a submit button for a week

I want to allow someone to update a field in a database once a week, i want to disable the submit button 我想允许某人每周一次更新数据库中的字段,我想禁用“提交”按钮

var enableSubmit = function() {
    console.log(this);
    $(this).removeProp("disabled");
}

$("#test").click(function() {
    $(this).prop("disabled", true);
    setTimeout(enableSubmit, 100000000000);
});

would work, obviously a bigger timer is needed but each time he refreshes the page, the timer would reset, I know ideally you would want to do that in a server side script, but security isn't an issue now, still if you prefer php, then i have no problem. 可以工作,显然需要一个更大的计时器,但是每次他刷新页面时,计时器都会重置,我知道理想情况下,您希望在服务器端脚本中执行此操作,但是现在安全性不是问题,即使您愿意PHP的,那么我没有问题。

Save the last submitted timestamp in a cookie or localStorage, have the button set to disabled on rendering and enable it with JavaScript if the last submitted timestamp in the cookie/localStorage has passed the 1-week mark. 将最后一次提交的时间戳保存在cookie或localStorage中,如果在cookie / localStorage中最后一次提交的时间戳已超过1周标记,则在渲染时将button设置为禁用,并使用JavaScript启用该button

$("#test").click(function() {
    $(this).prop("disabled", true);
    localStorage.setItem('last_submitted_time', new Date().getTime());
});

And then in your $(document).ready() or some initializer you might have 然后在$(document).ready()或某些初始化程序中

var now = new Date().getTime();
var lastSubmittedTime = +localStorage.getItem('last_submitted_time') || 0;
if (now - lastSubmittedTime >= 604800000) {
    // Enable the submit button.
}

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

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