【发布时间】:2019-12-23 10:56:02
【问题描述】:
我的页面上有两个按钮。如果用户单击一个,则另一个将被禁用,反之亦然。假设如果启用了 'Button1'。这意味着 'Button2' 被禁用。现在,当我点击“按钮 2”时......“按钮 1”被禁用。 但是,当我刷新页面时,“Button1”被启用,“Button2”再次被禁用。如何存储上次禁用按钮的状态并在刷新页面后查看?
这是我迄今为止尝试过的:
template.html:
<button type="button" id ='button1'> Button 1 </button>
<br> <br>
<button type="button" id ='button2' disabled /> Button 2 </button>
template.js(使用 Jquery):
$('#button1').click(function () {
$("#button1").attr("disabled", true);
$("#button2").attr("disabled", false);
$('#button2').click(function () {
$("#button2").attr("disabled", true);
$("#button1").attr("disabled", false);
刷新页面后如何存储禁用按钮的最后状态?
【问题讨论】:
-
您可以使用 localStorage/sessionStorage、cookies 或服务器端数据存储(通过 AJAX 访问)来保持状态。哪种情况最适合您的情况取决于您希望状态保持多长时间,以及它需要有多安全
-
将值存储到浏览器本地存储中并从那里加载
-
@RoryMcCrossan 感谢您的回复。我是 JS/jQuery 新手,请问可以要求做一个小演示吗?
标签: javascript jquery html