简体   繁体   English

HTML5 / JavaScript:在localStorage中存储动态变量名称和值

[英]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. 我想存储的价值yes为localStorage的一个关键movie1使用JavaScript。 However, I want the 1 part of the key to be dynamic based on whatever movieID is set to. 但是,我希望键的1部分是动态的,基于设置的movieID

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: 您可以使用setItem函数将值存储在本地存储中:

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. 实际上是图书馆使用的帮手,不是官方的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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