简体   繁体   中英

HTML5/JavaScript: Store Dynamic Variable Name and Value in localStorage

I would like to store the value yes into localStorage with a key of movie1 using javascript. However, I want the 1 part of the key to be dynamic based on whatever movieID is set to.

This is what I currently have, but it isn't working:

movieID = 1;

localStorage.movie + movieID = 'yes';

Any thoughts or insight on how I can accomplish this would be greatly appreciated. Thanks!

试试这个:

localStorage['movie'+movieID] = 'yes';

You can use the setItem function to store values in local storage:

localStorage.setItem('movie' + movieID, 'yes');

Then later when you want to check the value, you can use

localStorage.getItem('movie' + movieID);

I think

localStorage.setItem('movie' + movieID, 'yes');

is actually a helper in a library usage and is not official.

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