gpt4 book ai didi

Javascript:创建动态变量和对象名称./html5 localstorage

转载 作者:行者123 更新时间:2023-11-28 05:03:39 25 4
gpt4 key购买 nike

我正在处理由 HTML5 本地存储保存的表单。

当按下保存时:

function saveAll(){
var field1 = document.getElementById('field1').value;
localStorage.setItem('con_field1',field1);
var field2 = document.getElementById('field2').value;
localStorage.setItem('con_field2',field2);
var field3 = document.getElementById('field3').value;
localStorage.setItem('con_field3',field3);
var field4 = document.getElementById('field4').value;
localStorage.setItem('con_field4',field4);
var field5 = document.getElementById('field5').value;
localStorage.setItem('con_field5',field5);
var field6 = document.getElementById('field6').value;
localStorage.setItem('con_field6',field6);
}

加载页面时(填写表格):

function ShowAll() {
var field1 = localStorage.getItem('con_field1');
document.conditioning.field1.value = field1;
var field2 = localStorage.getItem('con_field2');
document.conditioning.field2.value = field2;
var field3 = localStorage.getItem('con_field3');
document.conditioning.field3.value = field3;
var field4 = localStorage.getItem('con_field4');
document.conditioning.field4.value = field4;
var field5 = localStorage.getItem('con_field5');
document.conditioning.field5.value = field5;
var field6 = localStorage.getItem('con_field6');
document.conditioning.field6.value = field6;
}

一切正常,但我想以更花哨、更高效的方式重写它。我在想这样的事情:

function ShowAll() {
var field = [];
for (i=0; i<6; i++) {
field[i] = localStorage.getItem(window['con_field' + i]);
document.purpose.field[i].value = window['con_field' + i]
}
}

但是浏览器并不喜欢这个。基本上我需要创建一个循环,自动将“字段”名称更改为“field1、field2、field3”等。窗口的东西正在工作,但我只是用错了。

有人有想法吗?

非常感谢!

最佳答案

function showAll(t1,c1,d1)  {
var field1 = localStorage.getItem('con_field1');
console.log(field1)
var field2 = localStorage.getItem('con_field2');
var field3 = localStorage.getItem('con_field3');
}

关于Javascript:创建动态变量和对象名称./html5 localstorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11399923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com