2016-01-26 52 views
1

我试图保存窗体(未验证)的细节,并将它们保存在JAVASCRIPT的localStorage中,我已经完成了。但是,当我重新加载浏览器并单击提交时,应该保存的以前的值不会保存在'数组'中,并且空白表单被替换。我知道数组和对象(var client_Array = [];var clientObj = {};)在我加载浏览器时被重新初始化,但不是数组应该保留旧的细节的副本(比如在索引0中),并将索引1中的空白或新副本?。这似乎并没有发生。 此代码仅适用于chrome而不适用于FF(在'load'btn中单击时不显示控制台中的详细信息)。 ...请帮助。提前致谢。阵列没有保存在本地存储

<head> 
<style> 
form { 
    width:300px; 
} 
input { 
    border:1px solid #FF9933; 
    width:225px; 
    height:24px; 
    margin-bottom:7px; 
} 
textarea { 
    border:1px solid #FF9933; 
    width:225px; 
    height:100px; 
} 
.fbtn { 
    width:80px; 
    margin-top:10px; 
    float:right; 
    text-align:center; 
    color:#FF6600; 
    font-weight:bold; 
} 
</style> 
</head> 
<body> 
<form> 
    <table border="0"> 
    <tr> 
     <td width="64">Name</td> 
     <td width="8">:</td> 
     <td><input type="text" id="inp_name" ></td> 
    </tr> 
    <tr> 
     <td>Email</td> 
     <td>:</td> 
     <td><input type="text" id="inp_email" ></td> 
    </tr> 
    <tr> 
     <td>Phone</td> 
     <td>:</td> 
     <td><input type="text" id="inp_phone"></td> 
    </tr> 
    <tr> 
     <td>Address</td> 
     <td>:</td> 
     <td><textarea id="inp_address"></textarea> 
     </td> 
    </tr> 
    </table> 
    <input type="button" onClick="sendDet()" class="fbtn" value="Submit"> 
    <br> 
    <br> 
    <br> 
    <input type="button" onClick="loadit()" class="fbtn" value="Load"> 
    <br> 
    <br> 
    <input type="reset" class="fbtn" value="reset"> 
</form> 
<script> 

var client_Array = []; 
var clientObj = {}; 



function sendDet(){ 
var clientObj = { 
name : document.getElementById('inp_name').value, 
    email : document.getElementById('inp_email').value, 
    phone : document.getElementById('inp_phone').value, 
    address : document.getElementById('inp_address').value 
} 

client_Array.push(clientObj) 

localStorage.setItem("client", JSON.stringify(client_Array)) 


} 

function loadit(){ 
var show = JSON.parse(localStorage.getItem("client")) 
console.log(show) 
} 


</script> 
</body> 
+0

从我理解你想要的不是覆盖新值,而是保留它们在具有旧条目的数组中,不是吗? –

+0

您可以使用garlic.js来保存数据。 –

+0

@达特巴图..色... –

回答

0

而不是client_Array.push,你应该使用client_Array.unshift。推在数组的末尾添加项目,而unshift将它添加为第一个元素(索引0顺便说一句,不是1)