简体   繁体   中英

sessionStorage.get() - get current key value

I have a current sessionStorage in place. The .set method is working correctly.

The problem is the key it's not fixed. it's being created with the array of the first product I click.

like this: 在此处输入图片说明

Like this, I can't have a key value to use the .get method in another page.

This is my JS.

 $('.go-to-bag').click(function() { var productId = $(this).parent().attr('id'); var productName = $(this).parent().attr('product-name'); var productQty = $(this).prev().val(); var productLWeight = $(this).parent().attr('product-weight'); var productPesoBruto = $(this).parent().attr('product-pesobruto'); var productPrice = $(this).parent().attr('product-price'); var productPricesemiva = $(this).parent().attr('product-pricesemiva'); var newList = new Object(); if (sessionStorage.newShoppingList) { newShoppingList = JSON.parse(sessionStorage.getItem('newShoppingList')); } else { newShoppingList = [] } newShoppingList.push({ productId, productName, productQty, productPrice, productPricesemiva, productLWeight, productPesoBruto }); sessionStorage.setItem('newShoppingList', JSON.stringify(newShoppingList)); var retrievedObject = sessionStorage.getItem('newShoppingList'); console.log('retrievedObject: ', JSON.parse(retrievedObject)); }); 

So, I can I get the key so i can use .get method and retrive the value inserted in another page?

The example that you've given us is correct. At this point I would review if that is truly the only place that you call .setItem .

Also, that last question makes me wonder -- are you intending to use localStorage instead of sessionStorage ? See this link for the difference

W3Schools Web Storage Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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