簡體   English   中英

頁面刷新后不保存JSON本地存儲

[英]JSON Local Storage not saving after the page refresh

新手在這里。 我正在開發一個具有數組的項目,該項目允許用戶通過在文本框中鍵入新項目然后單擊“提交”按鈕來將新項目添加到數組中。 我做到了,這樣當我卸載頁面時該數組將轉換為JSON,而在再次加載該頁面時將轉換回JavaScript。 我的腳本可以將項目保存到本地存儲一段時間。 經過一些代碼重構后,程序突然停止保存我的項目在頁面刷新中。 我想對它進行編程,以便即使關閉計算機也可以保存它。

我嘗試過在線搜索解決我的問題的方法,但是似乎沒有人遇到類似的問題。

我的代碼:

<body onload="Items" onunload="JSONItems">

    <textarea id="type" rows="2" cols="25"></textarea>

    <button id="submit" type="button">Submit</button>

    <script type="text/javascript">

        var Items = JSON.parse(localStorage.getItem("Items"))
        var JSONItems = localStorage.setItem("Items", JSON.stringify(Items))

        if (!Items) Items= []

        $("#submit").click(function() {
            var newitem = $("#type").val()
            if (newitem) {
                Items.push(newitem)
                $("#type").val("")
            }
        })

    </script>

</body>

你能告訴我嗎?

我懷疑問題是由於2個不同的鍵造成的。 您正在使用TemasEFrases作為設置項目的鍵,但是在檢索時,您正在使用Items作為鍵。 嘗試在兩個地方使用Items

假設您正在使用jQuery進行DOM操作,那么我已經在JS Bin上創建了一個有效的代碼段。

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <body>

    <textarea id="type" rows="2" cols="25"></textarea>

    <button id="submit" type="button">Submit</button>

    <p>Items:</p>
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
    <ul id="result"></ul>
</body>
</html>

JS

$(document).ready(function () {
  var localstorageItems = JSON.parse(localStorage.getItem('Items'));
  var items = localstorageItems || [];

  showItems();


  $('#submit').click(function(event) {
    event.preventDefault();

    var newitem = $("#type").val()

    if (newitem) {
      items.push(newitem);
      $('#type').val('');

      localStorage.setItem('Items', JSON.stringify(items));
      showItems();
    }
  });

  function showItems () {
    if (items && items.length > 0) {
      var result = items.map(function (item) {
        return '<li>'+item+'</li>';
      });

      $('#result').html(result);
    }
  };
});

這是工作代碼https://jsbin.com/yoqoyoxiza/edit?html,js,output的鏈接

是的,我們在代碼方面遇到了一些問題。 這樣的片段效果很好。

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  </head>
  <body onload="LoadItems()" onunload="saveItems()">
    <div>
      <textarea id="inputBox" rows="2" cols="25"></textarea>
      <button id="submit" type="button">Submit</button>
    </div>
    <div>
      <p>Data in storage</p>
    </div>
    <div>
      <textarea id="showData" rows="2" cols="25"></textarea>
    </div>

    <script type="text/javascript">
      var Items = [];
      function LoadItems() {
        debugger;
        Items = JSON.parse(localStorage.getItem("Items"));
        if (!Items) Items = [];
        $("#showData").val(JSON.stringify(Items));
      }

      function saveItems() {
        var JSONItems = localStorage.setItem("Items", JSON.stringify(Items));
      }

      $("#submit").click(function() {
        var newitem = $("#inputBox").val();
        if (newitem) {
          Items.push(newitem);
          $("#inputBox").val("");
        }
      });
    </script>
  </body>
</html>

每次更改后,您需要將其保存到本地存儲中:

 $("#submit").click(function() {
        var newitem = $("#type").val();
        if (newitem) {
            Items.push(newitem);
            $("#type").val("");
            // HERE:
            localStorage.setItem("Items", JSON.stringify(Items));
        }
 })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM