如何为当前打开的标签实现本地存储?

我按照此链接将数据存储在我的浏览器中以获取单选按钮。问题是如果我在另一个选项卡中打开相同的 url,则单选按钮会根据上一个选项卡选择。


功能是 - 提交按钮重定向到另一个页面。所以,我需要的是本地存储该特定当前打开的选项卡和特定链接。


我正在使用的代码


<!doctype html>

<html>

<head>

<meta charset="UTF-8"/>

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />

<title>Save state of checkbox on refresh using JavaScript</title>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />

<link href="css/custom.css" rel="stylesheet" />

<style type="text/css">

</style>

</head>

<body>


<ul class="list-group">

      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox" value="asdf"> Must I save my state?</li>

      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="anothercheckbox" value="1"> Try and save this</li>

      <li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox2" value="qwer"> This can be saved as well.</li>

    </ul>  

<script>

// Avoid scoping issues by encapsulating code inside anonymous function

(function() {

 // variable to store our current state

  var cbstate;


  // bind to the onload event

  window.addEventListener('load', function() {


    // Get the current state from localstorage

    // State is stored as a JSON string

    cbstate = JSON.parse(localStorage['CBState'] || '{}');


    // Loop through state array and restore checked 

    // state for matching elements

    for(var i in cbstate) {

      var el = document.querySelector('input[name="' + i + '"]');

      if (el) el.checked = true;

    }


    // Get all checkboxes that you want to monitor state for

    var cb = document.getElementsByClassName('save-cb-state');


墨色风雨
浏览 191回答 2
2回答

MMTTMM

您应该能够使用 sessionStorage 而不是 localStorage 来管理它。有关此处差异的更多信息:https ://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

喵喔喔

只需将内容 localstorage 更改为 sessionStorage。以下是此问题的更新代码和解决方案(如何为当前打开的选项卡实现本地存储?)。答案是:<script>// Avoid scoping issues by encapsulating code inside anonymous function(function() {&nbsp;// variable to store our current state&nbsp; var cbstate;&nbsp; // bind to the onload event&nbsp; window.addEventListener('load', function() {&nbsp; &nbsp; // Get the current state from localstorage&nbsp; &nbsp; // State is stored as a JSON string&nbsp; &nbsp; cbstate = JSON.parse(sessionStorage['CBState'] || '{}');&nbsp; &nbsp; // Loop through state array and restore checked&nbsp;&nbsp; &nbsp; // state for matching elements&nbsp; &nbsp; for(var i in cbstate) {&nbsp; &nbsp; &nbsp; var el = document.querySelector('input[name="' + i + '"]');&nbsp; &nbsp; &nbsp; if (el) el.checked = true;&nbsp; &nbsp; }&nbsp; &nbsp; // Get all checkboxes that you want to monitor state for&nbsp; &nbsp; var cb = document.getElementsByClassName('save-cb-state');&nbsp; &nbsp; // Loop through results and ...&nbsp; &nbsp; for(var i = 0; i < cb.length; i++) {&nbsp; &nbsp; &nbsp; //bind click event handler&nbsp; &nbsp; &nbsp; cb[i].addEventListener('click', function(evt) {&nbsp; &nbsp; &nbsp; &nbsp; // If checkboxe is checked then save to state&nbsp; &nbsp; &nbsp; &nbsp; if (this.checked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cbstate[this.name] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; // Else remove from state&nbsp; &nbsp; &nbsp; &nbsp; else if (cbstate[this.name]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete cbstate[this.name];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; // Persist state&nbsp; &nbsp; &nbsp; &nbsp; sessionStorage.CBState = JSON.stringify(cbstate);&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; });})();</script>特别感谢@Jacob Nelson
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript