您可以使用这种方式作为基础。这仅使用js。在客户端本地存储上保存为 json 比保存为 xml 更好。没有 JQuery:
<form>
<input id="txt1" onchange="saveDraft()" type="text" />
<input id="txt2" onchange="saveDraft()" type="text" />
<input id="txt3" onchange="saveDraft()" type="text" />
<input id="btnSubmit" type="submit" value="submit" />
</form>
<script type="text/javascript">
var elements = ["txt1","txt2","txt3"];
function saveDraft(){
var values= new Object;
for(var i=0;i<elements.length;i++){
values[elements[i]] = document.getElementById(elements[i]).value;
}
localStorage["draft"] = JSON.stringify(values);
}
window.onload = function (){
restoreDraft();
}
function restoreDraft(){
var values;
if(localStorage["draft"] ){
values = JSON.parse(localStorage["draft"]);
}
else
return;
if(confirm('Do you want to restore saved draft?')){
for(var i=0;i<elements.length;i++){
document.getElementById(elements[i]).value = values[elements[i]];
}
localStorage["draft"] ='';
}
}
</script>