我有一个循环,它使用 localstorage 来存储非空白 ID #M1 到 #M48 但还需要一种方法来检索 localstorage 变量。我怀疑答案与 JSON.stringify 有关。
我怎样才能做到这一点?
<form>
<input type = "text" id = "M1" />
<input type = "text" id = "M2" />
<input type = "text" id = "M3" />
...
</form>
<script> //store the variables
$max_entrants = 48;
for ($i = 0; $i <= $max_entrants; $i++) {
localStorage.setItem("LS-MEM_" +$i, "M" +$i);
}
</script>
<script> //retrieve the variables into a form using ID#s
$max_entrants = 48;
for ($i = 0; $i <= $max_entrants; $i++) {
$wrench = localStorage.getItem("LS-MEM_" +$i);
$("#M" +$i).val($wrench);
}
</script>
上面的脚本已经过测试并且失败了。
<div id = "result"></div>
<script>
document.getElementById("result").innerHTML = localStorage.getItem("LS-MEM_2");
</script>
输出是 ======>> "M2"
这可以解决吗?
好吧,字符串“M2”是您存储在本地存储中的内容。尝试这样的事情(假设你使用 jQuery)来存储值。
localStorage.setItem("LS-MEM_" +$i, $jQuery("#M"+$i).val() );
链接到小提琴:https : //jsfiddle.net/iPirat/26tjsva1/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句