请帮助,在尝试通过 sessionStorage 存储数据时遇到困难
我有一个简单的表单,希望从表单输入中读取数据并将其附加到表中,并在会话期间将该数据存储在表中
HTML
Name: <input type="text" id="name"/>
Email: <input type="text" id="email"/>
Tel: <input type="text" id="tel"/>
Street: <input type="text" id="street"/>
City: <input type="text" id="city"/>
State: <input type="text" id="state"/>
Zip: <input type="text" id="zip"/>
<button id="myForm" type="button">Add Costumer</button>
JS
$(document).ready(function(){
if(sessionStorage.length>0){
display()
}
})
$("#myForm").on("click", function(){
save()
$(":input").val(""); //clean fields of the form
});
function save(){
var name= $("#name").val();
var email= $("#email").val();
var telephone= $("#tel").val();
var street= $("#street").val();
var city= $("#city").val();
var state= $("#state").val();
var zip= $("#zip").val();
var inputArray = [name, email, telephone, street, city, state, zip];
for(i in inputArray){//storing input data
sessionStorage.setItem(i, inputArray[i])
};
display()
}
function display(){
var restoredName = sessionStorage.getItem(0);
var restoredEmail = sessionStorage.getItem(1);
var restoredTel = sessionStorage.getItem(2);
var restoredStreet = sessionStorage.getItem(3);
var restoredCity = sessionStorage.getItem(4);
var restoredState = sessionStorage.getItem(5);
var restoredZip = sessionStorage.getItem(6);
//append filled information from the form to the table and 2 buttons - Update and Remove
$("#listContent table").append( "<tr>" +
"<td>" + restoredName + "</td>"+
"<td>" + restoredEmail + "</td>"+
"<td>" + restoredTel + "</td>"+
"<td>" + restoredStreet + "</td>"+
"<td>" + restoredCity + "</td>"+
"<td>" + restoredState + "</td>"+
"<td>" + restoredZip + "</td>"+
"<td>" + "<button>Update</button>" + "</td>" +
"<td>" + "<button>Remove</button>" + "</td>" +
"</tr>");
}
问题是只有最后一次提交被存储并显示在表格中,而不是在会话期间执行的所有提交
我猜 sessionStorage 每次用户点击提交时都会被重写,并带有新的输入值。而且我不知道如何在每次新提交时增加存储空间。请指教,如何解决这个问题?谢谢