javascript'对象对象'来自localstorage

时间:2016-08-26 08:02:18

标签: javascript jquery html arrays json

当我执行[object] object功能

时,我收到一条警告showlogsf
 var fdata = {fidval, fweightval, feggslaidval, fgraineatenval, fwaterval};
 var fidval = document.getElementById('#fid');
 var fweightval = document.getElementById('#fweight');
 var feggslaidval = document.getElementById('#feggslaid');
 var fgraineatenval = document.getElementById('#fgraineaten');
 var fwaterval = document.getElementById('#fwater');

这些是可以通过按钮点击触发的两个功能

$('#submitf').click(function (){
  localStorage.setItem ("fdatak", JSON.stringify(fdata));
});

$('#showlogsf').click(function(){
  var fdataload = JSON.parse(localStorage.getItem("fdatak"));
  alert(fdataload);
 });

});

3 个答案:

答案 0 :(得分:0)

您正在将对象作为警报推送。 HTML将其显示为[object Object]。您必须解决此对象的实际属性,因此:

$('#showlogsf').click(function(){
    var fdataload = JSON.parse(localStorage.getItem("fdatak"));
    alert(fdataload.fidval);
    alert(fdataload.fweightval);
    alert(fdataload.feggslaidval);
    // etc etc...
});

编辑:似乎您还要保存文档元素,而不是值。我假设fdataload.properties是实际值,您应该获取原始HTML数据或输入此值而不是HTML元素本身

答案 1 :(得分:0)

我猜你的fdata是一个数组吗?

试试这个var fdata = [fidval, fweightval, feggslaidval, fgraineatenval, fwaterval];

答案 2 :(得分:0)

  

我将数组的值编辑为xxx.value而不是xxx

var fdata =  new Array();   

$('#submitf').click(function (){

var fidval = document.getElementById('fid');
var fweightval = document.getElementById('fweight');
var feggslaidval = document.getElementById('feggslaid');
var fgraineatenval = document.getElementById('fgraineaten');
var fwaterval = document.getElementById('fwater'); 

var fdata =  [];
  像这样

fdata.push (fidval.value, fweightval.value, feggslaidval.value, fgraineatenval.value, fwaterval.value);




localStorage.setItem ("fdata", JSON.stringify(fdata));


});



$('#showlogsf').click(function(){




var fdataload = JSON.parse(localStorage.getItem('fdata'));
  document.getElementById("flogview").innerHTML = fdataload;

});
  

现在它保存但我还有另外一个问题,为什么每次点击showlogsf按钮都会替换数据?