简体   繁体   English

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

[英]Local Storage on page refresh returns empty value

Local storage returns an empty string after page refresh on woocomerce not sure why code below returns an empty value when there should be the value stored inside: 在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();

}

Anytips are helpful thanks in advance. Anytips很有帮助,谢谢。

You're using it wrong, this is how you set and retreive items: 您使用的方法有误,这是您设置和撤回物品的方式:

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

So in your case, eg: 因此,在您的情况下,例如:

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

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

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