页面刷新时的本地存储返回空值

时间:2017-06-05 09:30:02

标签: javascript jquery woocommerce local-storage

本地存储在woocomerce页面刷新后返回一个空字符串,不确定为什么应该存储内部的值时,下面的代码返回一个空值:

$(document).ready(function() {
  $(window).keyup(saveSettings);
  if (window.localStorage) {
    loadSettings();
  }
});

function loadSettings() {
  $('#billing_first_name').val(localStorage.bfn);
  $('#billing_last_name').val(localStorage.bln);
  $('#billing_company').val(localStorage.cn);
  $('#billing_phone').val(localStorage.phone);
  $('#billing_email').val(localStorage.ea);
}

function saveSettings() {
  localStorage.bfn = $('#billing_first_name').val();
  localStorage.bln = $('#billing_last_name').val();
  localStorage.cn = $('#billing_company').val();
  localStorage.phone = $('#billing_phone').val();
  localStorage.ea = $('#billing_email').val();

}

提前感谢任何提示。

1 个答案:

答案 0 :(得分:3)

您使用错误,这就是您设置和检索项目的方式:

localStorage.setItem('name', 'value');
var value = localStorage.getItem('name');
console.log(value); //value

所以在你的情况下,例如:

localStorage.setItem('bfn', $('#billing_first_name').val()); //Set value
$('#billing_first_name').val(localStorage.getItem('bfn')); //Get value