同样是使用sessionStorage,在unonload将被checked的按钮的id,和text的id与value存入sessionStorage中(具体怎么存看你的需求),最后在onload时将sessionStorage中的数据归还。
function saveSession(){
sessionStorage.clear();
for (var i = 1; i <=27; i++) {
var div=document.getElementById(i);
sessionStorage.setItem(i,div.style.display);
var tk=document.getElementById('z'+i);
if (tk!=null){
sessionStorage.setItem('z'+i,tk.style.display+tk.value);
}
}
var inputs=document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type=='radio'&&inputs[i].checked){
sessionStorage.setItem(inputs[i].id,'radio');
}else if (inputs[i].type=='checkbox'&&inputs[i].checked){
sessionStorage.setItem(inputs[i].id,'checkbox');
}else if (inputs[i].type=='text'&&!inputs[i].id.includes('z')){
sessionStorage.setItem(inputs[i].id,inputs[i].value);
}
}
}
function reload(){
// console.log(sessionStorage);
for (var i = 1; i <=27; i++) {
var div=document.getElementById(i);
div.style.display=sessionStorage[i];
sessionStorage.removeItem(i);
var tk=document.getElementById('z'+i);
if (tk!=null){
if (sessionStorage['z'+i][0]=='n'){
tk.style.display='none';
tk.value=sessionStorage['z'+i].substring(4,);
}else{
tk.style.display='block';
tk.value=sessionStorage['z'+i].substring(5,);
}
sessionStorage.removeItem('z'+i);
}
}
var sessionKeys = Object.keys(sessionStorage)
for (var i=0;i<sessionKeys.length;i++) {
if (sessionStorage.getItem(sessionKeys[i])=='radio'){
document.getElementById(sessionKeys[i]).checked=true;
}else if (sessionStorage.getItem(sessionKeys[i])=='checkbox'){
document.getElementById(sessionKeys[i]).checked=true;
}else{
//要求text输入的字串不能为radio或checkbox
document.getElementById(sessionKeys[i]).value=sessionStorage.getItem(sessionKeys[i]);
}
}
}
window.onunload=saveSession;
window.onload=reload;