繁体   English   中英

使用JavaScript启用/禁用按钮

[英]Enable/disable button using javascript

我正在尝试构建一个简单的html应用程序,以获取一些基本的客户信息并将该信息存储在数据库中。 捕获信息之后,当客户登录其个人资料时,他可以看到3个按钮。

button 1= print

button 2= delete

button 3= edit

特殊要求:

  • 除非点击按钮1,
  • 然后,应该在用户首次登录时禁用按钮2和3。

对于下一次后续登录

  • 应该启用所有按钮。

到目前为止,我已经做了以下事情:

<html>
<head>
    <script>
        function enableButton() {
          document.getElementById("button2").disabled = false;
          document.getElementById("button3").disabled = false;
        } 
    </script>
</head>
<body>
    <input type="button" id="button1" value="print" onclick="enableButton()"  />
    <input type="button" id="button2" value="delete" disabled />
    <input type="button" id="button3" value="edit" disabled />
</body>
</html>

但这并不能满足上述要求。 任何帮助,将不胜感激

使用HTML5 localStorageJavascript Cookie来满足要求,因为它完全基于用户登录。

您必须遵循的步骤:

  1. 首次登录时,请在本地存储中创建一个新的cookie或setItem作为新的。
  2. 用户第二次登录时,将cookie值或setItem在localstorage中设置为旧。

通过这样做,您可以确定哪个是新用户,哪个是旧用户,并根据该功能执行功能。

使用localStorage可能是一种解决方法,因为您没有描述服务器端会话检索的任何使用

var trueOnFirstVisit = window.localStorage.firstVisit || "true";   
$("#button2, #button3").prop("disabled", trueOnFirstVisit);
window.localStorage.firstVisit = "false";

因此,如果我们的localStorage值为"false"表示用户已经访问了该页面。

https://developer.mozilla.org/en/docs/Web/API/Window/localStorage
http://api.jquery.com/prop/

假设您已经在用户登录时存储了信息,则可以使用$.cookie('varible_name');访问该信息$.cookie('varible_name'); 如果您使用过Cookie。

您还可以发送发布请求并使用rpc $ .post(...)并检查用户是否登录。如果后续登录意味着单独登录而不是持久登录,则可以在数据库的登录表中保留一个计数器并根据需要更新/获取。

暂无
暂无

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

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