检索javascript localstorage中的最后一个键 [英] retrieving last key in javascript localstorage

查看:42
本文介绍了检索javascript localstorage中的最后一个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个大学作业,所以请原谅这里知识的匮乏.我的任务是为客户"创建一个基于Web的应用程序,要求他们在完成所有详细信息后打开发票,到目前为止,我的表格工作正常,我可以毫无问题地保存到本地存储中,甚至可以以我想要的格式检索信息.

I am doing a uni assignment so please excuse the coplete lack of knowledge here. I have been tasked with creating a web based app for a "client" that requires me to open an invoice after they have completed all the details, so far I have the form working fine, i can save to localstorage without an issue and can even retrieve the information in the format that I want.

问题是,如果我有多个订单",那么我只能检索保存到localstorage的第一个字符串,而且我不知道如何使它显示最新/最后一个.

The issue is that if i have multiple "orders' than i can only retrieve the first string saved to localstorage and i cannot figure out how to make it display the newest/last one.

键是从日期得出的几乎随机生成的数字,因为我们不得不承担大量订单.我已经附上了下面的代码,所以如果有人可以帮助我将不胜感激.我也不能使用Jquery或JSON或类似的东西.我们被告知我们无法使用这些功能.我也不能使用服务器(php等).

The key is an almost randomly generated number derived from the date as we had to assume a large quantity of orders. I have attached the code below so if anyone can help i would appreciate it. Also I cannot use Jquery or JSON or anything like that. We have been told we are not able to use these. Also i cannot use a server (php etc).

谢谢 史蒂夫

<script>
var i = 0;

var itemKey = localStorage.key(i);
var values = localStorage.getItem(itemKey);
values = values.split(";");
var name = values[0];
var company = values[1];
var contactnumber = values[2];
var email = values[3];
var address1 = values[4];
var address2 = values[5];
var suburb = values[6]
var postcode = values[7];
var comments = values[8];
var bags = values[9];
var distance = values[10];
var hdelivery_fee = values[11];
var hprice = values[12];
var htotal_notax = values[13];
var hgst = values[14];
var htotal_tax = values[15];
var hordernumber  = values[16];


document.write('Name: ' + name + '<br />');
document.write('Company: ' + company + '<br />');
document.write('Contact: ' + contactnumber + '<br />');
document.write('Email; ' + email + '<br />');
document.write('Address; ' + address1 + '<br />');
document.write('Address; ' + address2 + '<br />');
document.write('Suburb; ' + suburb + '<br />');
document.write('Postcode; ' + postcode + '<br />');
document.write('Comments; ' + comments + '<br />');
document.write('Number of Bags; ' + bags + '<br />');
document.write('Distance; ' + distance + '<br />');
document.write('Delivery Fee; $' + hdelivery_fee + '<br />');
document.write('Price of Bags; $' + hprice + '<br />');
document.write('Total Ex-GST; $' + htotal_notax + '<br />');
document.write('GST; $' + hgst + '<br />');
document.write('Total Inc GST; $' + htotal_tax + '<br />');
document.write('hordernumber; ' + hordernumber + '<br />');


</script>

推荐答案

要显示数组的最后一个值,应使用values [values.length-1].

To display the last value of an array you should use values[values.length-1].

这将确保您获得绝对的最后一个.

This will guarantee you get the absolute last one.

这篇关于检索javascript localstorage中的最后一个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆