[英]how to save toggle button state on refresh using javascript
Hey everyone I am having difficulty keeping the state of my toggle button on when i refresh the page, it is stored in localStorage correctly, but even after several attempts, I wasn't able to call the function to execute on page refresh大家好,当我刷新页面时,我很难保持我的切换按钮的 state 处于打开状态,它正确存储在 localStorage 中,但即使经过多次尝试,我也无法调用 function 在页面刷新时执行
save.addEventListener('click', () => { localStorage.setItem("checkbox1", checkBox1.checked); localStorage.setItem("checkbox2", checkBox2.checked); localStorage.setItem('timezone', timezone.selectedIndex); } function isChecked() { const checkBox1 = document.getElementById('checkbox1'); if (document.getElementById('checkbox1').checked == false) { localStorage.getItem('checkbox1'); } } window.onload = function() { isChecked(); }; }
<form> <div class="toggle-switch-1"> <p class="notification-1">Send Email Notifications</p> <!-- toggle switch goes here --> <label type="button" class="switch-light switch-candy"> <input id="checkbox1" type="checkbox"> <span> <span id="off">Off</span> <!-- <span id="on1">On</span> --> <!-- <button onclick="check()" id="on1">On</button> --> <span id="on1">On</span> <a></a> </span> </label> </div> <div class="toggle-switch-2"> <p class="notification-2">Send Profile to Public</p> <!-- toggle switch goes here --> <label class="switch-light switch-candy" onclick=""> <input id="checkbox2" type="checkbox"> <span> <span id="off">Off</span> <span id="on2">On</span> <a></a> </span> </label> </div> <select class="form-field" id="timezone"> <option disabled selected>Select Timezone</option> <option>Eastern Standard Time (EST)</option> <option>Pacific Standard Time (PST)</option> <option>Central Standard Time (CST)</option> <option>Mountain Standard Time (MST)</option> </select> <div class="settings-button"> <button type="button" class="button-primary" id="save">Save</button> <button class="button-disabled" id="cancel">Cancel</button> </div> </form>
Im trying to save the state of the button on refresh (clicking save button) but it defaults to off when I refresh as well as the drop down list of timezones, if anyone can help clear this up, I would appreciate it Thank you!!我试图在刷新时保存按钮的 state(单击保存按钮),但是当我刷新以及时区的下拉列表时它默认为关闭,如果有人可以帮助清除它,我将不胜感激谢谢!
You are just getting the value from the local storage.您只是从本地存储中获取价值。 But not actually assigning it to any toggle buttons.但实际上并未将其分配给任何切换按钮。 You need to assign them to make it work您需要分配它们以使其工作
function isChecked() { document.getElementById('checkbox1').checked = localStorage.getItem('checkbox1') === 'true'); }
This is how it works:这是它的工作原理:
Get the value out of local storage.从本地存储中获取价值。
The value saved in the local storage would be of type string.保存在本地存储中的值将是字符串类型。
So we need to convert it to Boolean value to make it work.所以我们需要将其转换为 Boolean 值才能使其工作。 The === check will do that for you. === 检查将为您做到这一点。
Finally assign it to the checkbox.最后将其分配给复选框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.