简体   繁体   English

如果在一天内点击一次并使用JavaScript和AngularJS 1在第二天启用按钮,如何禁用按钮?

[英]How to disable a button, if it is clicked once in a day and enable on next day using JavaScript and AngularJS 1?

My button should be disabled once I clicked in a day (ie) only once in a day a user enter the details using submit button. 一旦我在一天中点击(即)一天一次用户使用提交按钮输入详细信息,我的按钮应该被禁用。 Next day automatically button should be enabled. 第二天应启用自动按钮。

Help me with JavaScript or AngularJS.. 帮助我使用JavaScript或AngularJS ..

If you have backend you could maintain state in DB as Sajal said, but since you are asking about angularjs/javascript and frontend solution cookies (what Chris said) or localStorage is way to go, set the cookie and based on cookie you can set button each day to reset 如果你有后端,你可以在数据库中保持状态,因为Sajal说,但既然你在询问angularjs / javascript和前端解决方案cookie(Chris说的话)或localStorage是什么方法,设置cookie并根据cookie你可以设置按钮每天重置

more info on w3schools: 关于w3schools的更多信息:

https://www.w3schools.com/js/js_cookies.asp https://www.w3schools.com/html/html5_webstorage.asp https://www.w3schools.com/js/js_cookies.asp https://www.w3schools.com/html/html5_webstorage.asp

Keep in mind cookies can be modified so DB is best way if you want fullproof solution. 请记住,可以修改cookie,因此如果您需要全面的解决方案,DB是最好的方法。

You can save the status of the click in the localstorage, but this solution is unsafe. 您可以在本地存储中保存点击的状态,但此解决方案不安全。 You should add a server side control. 您应该添加服务器端控件。

You can use timeout functionality. 您可以使用超时功能。 You can give specific time for timeout then after that timeout you can enable the button. 您可以给出超时的特定时间,然后在超时后启用该按钮。

EX: EX:

var theInterval = $interval(function(){
      //Enable button
   }.bind(this), time);  

You can use localStorage : 您可以使用localStorage

<button ng-disabled="!buttonIsActive()" ng-click="onClick()"></button>

$scope.buttonIsActive = function() {
    var now = new Date();
    var day = now.getDate();
    var lastClickDay = parseInt(localStorage.getItem("last-click-day"));        
    return day > lastClickDay;
}

$scope.onClick = function() {
    var now = new Date();
    var day = now.getDate();
    localStorage.setItem("last-click-day", day);
    // ... Do something else here
}

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

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