【问题标题】:How to keep the state of input checkbox on page reload如何在页面重新加载时保持输入复选框的状态
【发布时间】:2017-12-06 20:55:27
【问题描述】:

如何存储输入复选框当前状态的 cookie?每次重新加载页面时,状态都会恢复为默认值(未选中),我一直在嗡嗡作响。我什至使用了一个没有成功的库

https://github.com/js-cookie/js-cookie Cookie.set()Cookie.get() 但没有成功。

我也一直在阅读有关 localStorage JavaScript API 的信息,但似乎无法正常工作。

jQuery 代码:

$(function () {
    $('input[type="checkbox"]').click( () => {
        if ($(this).is(':checked')) {
            $('input[type="checkbox"]').prop('checked', true, localStorage.getItem('checked')).attr('checked', 'checked');
            localStorage.setItem('checked', 'checked');
            $('img').attr('src', '/full_logo_transparent.png');
            $('link#hueman-main-style-css').attr('href', '/darkstyle.min.css');
            console.log('Dark Mode enabled');
        } else {
            $('input[type="checkbox"]').prop('checked', false);
            $('link#hueman-main-style-css').attr('href', '/main.min.css');
        }
    })
});

这是为了记住输入复选框的状态并在页面重新加载时保存它,这样就不必每次都切换它(它会加载额外的 CSS 文件)

我错过了什么吗?顺序正确吗?

该站点是https://itmagazin.info,在右侧您会找到名称为“Noćni Režim”input:checkbox,它会激活黑暗模式,但点击后它不会保持这种状态在任何文章上。

感谢任何帮助。

【问题讨论】:

  • 您只是为那里的输入字段分配了一个点击处理程序 - 您没有做任何其他事情加载时 ...
  • 您将 localStorage.getItem('checked') 作为第三个参数传递给 JQuery .prop() 方法,该方法不接受 3 个参数。并且不需要设置.attr.prop
  • CBroe 好的,我需要添加window.onload() 或类似的东西吗?斯科特,我知道.attr 不是必需的,我只是在测试一些东西,我需要.prop,否则它不会在DOM 中加载添加的CSS 文件。
  • “所以我需要添加 window.onload() 或类似的东西吗?” - 不,因为您已经在使用$(function () {})。您只需要对其中的存储值进行实际做某事,而不仅仅是设置一个等待输入被点击的处理程序...
  • CBroe,好的,谢谢您的信息,非常感谢。

标签: javascript jquery cookies


【解决方案1】:

这比你做的要简单得多。只需在页面加载时获取最后存储的值,并根据被单击后的复选框状态进行适当的设置。

由于沙盒,以下代码在 Stack Overflow sn-p 环境中无法运行,但您可以对其进行测试here

$(function () {
  // Reset the checkbox to the last state on the last visit
  // The value in localStorage will be the strings "true", "false" or
  // it won't be there at all, in which case "" will be returned.
  // The opposite of "true" (or !"true") is the Boolean false and the
  // opposite of that (!!"true") is the Boolean true.  The opposite of
  // "" (!"") is the Boolean true and the opposite of that (!!"") is the
  // Boolean false
  $('#check').prop("checked", !!localStorage.getItem("darkMode"));

  $('#check').on("click", function() {
    if ($(this).is(":checked")) {
      localStorage.setItem('darkMode', 'true');
      $('img').attr('src', '/full_logo_transparent.png');
      $('link#hueman-main-style-css').attr('href', '/darkstyle.min.css');
      console.log('Dark Mode enabled');
    } else {
      localStorage.setItem('darkMode', 'false');
      $('link#hueman-main-style-css').attr('href', '/main.min.css');
      console.log("No Dark Mode");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="test" id="check"><label for="check">Item</label>

【讨论】:

  • 我试试看。谢谢。
  • 嗯.. 我相信这就是我要找的东西,但由于某种原因,输入现在无法点击。我已将您的代码放在itmagazin.info 网站上(“NOĆNI REŽIM”切换器)。哦,代码在“app.js >ln 39”
  • 很抱歉,您不应该期望我们出去调试实时网页。您可以从 Fiddle 中看到代码确实有效。如果在您将其与您的网站集成后它现在无法正常工作,则问题一定出在您网站上的一些其他代码上。我的答案(和小提琴)确实有一个错字,我已经更正了,但这不会导致你描述的问题。
  • 不存在的 localStorage 键不返回空字符串。同样!!'false'!!'true' 都返回true。可以使用 JSON.parse()` 转换为布尔值
猜你喜欢
  • 2016-08-23
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 2013-02-12
  • 2021-03-01
相关资源
最近更新 更多