[英]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);
http://jsfiddle.net/ZwhBY/ http://jsfiddle.net/ZwhBY/
Check out square brackets for properties. 查看方括号的属性。
http://www.jibbering.com/faq/faq_notes/square_brackets.html http://www.jibbering.com/faq/faq_notes/square_brackets.html
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.