简体   繁体   English

如何保持页面重新输入时输入框的状态

[英]How to keep the state of input checkbox on page reload

How do I store a cookie for input checkbox current state? 如何存储Cookie以用于输入复选框当前状态? Every time when the page reloads, the state is restored to default (unchecked) and I've been buzzing my head with this. 每次页面重新加载时,状态都会恢复为默认状态(未选中),并且我一直对此感到困惑。 I even used a library without any success 我什至没有成功使用图书馆

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

I've been also reading about localStorage JavaScript API but can't seem to getting working. 我也一直在阅读有关localStorage JavaScript API的信息,但似乎无法正常工作。

jQuery code: 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');
        }
    })
});

This is suppose to remember the state of the input checkbox and save it upon page reload so that it doesn't have to be toggled every single time (It loads additional CSS file) 假定记住输入复选框的状态并将其保存在页面重新加载时,这样就不必每次都切换它(它会加载其他CSS文件)

Am I missing something? 我想念什么吗? Is the order correct? 顺序正确吗?

The site is https://itmagazin.info and on the right you will find input:checkbox with the name "Noćni Režim" which activates the dark mode, but it doesn't stay that way after you click on any article. 该站点为https://itmagazin.info ,在右侧您将找到名为“NoćniRežim”的 input:checkbox ,该input:checkbox可激活暗模式,但是单击任何文章后,它都不会保持这种状态。

Any help is appreciated. 任何帮助表示赞赏。

This is much simpler than you are doing. 这比您做的要简单得多。 Just get the last stored value upon page load and set it appropriately based on the state of the checkbox after being clicked. 只需在页面加载时获取最后存储的值,并在单击后根据复选框的状态对其进行适当设置。

The following code won't work in the Stack Overflow snippet environment, due to sandboxing, but you can test it here . 由于沙箱操作,以下代码在Stack Overflow代码段环境中无法使用,但您可以在此处进行测试。

 $(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> 

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

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