我创建了两个html页面。一页有姓名和年龄文本字段,第二页有母姓和父姓。每当我点击第一页中的下一个按钮进入第二页时,如果我点击第一页的第二页上的前一个按钮但是没有显示数据。所以我想要那些字段中的数据。
我希望在我输入之前看到字段内的数据...
答案 0 :(得分:1)
您可以使用localStorage
存储值并检索它。
HTML
<form name="frm1" method="post">
<table>
<tr><td>Enter Your Name:</td>
<td><input type="text" id="name" name="name"></td></tr>
^^ id attribute added
<tr><td>Enter Your Age:</td>
<td><input type="text" id ="age" name="age"></td></tr>
^^ id attribute added
<tr>
<td></td>
<td>
<a href="page2.html">
<input type="button" id ="nextButton" value="Next">
^^ id attribute added
</a></td>
</table>
</form>
JS
// Check if local storage has previously stored value
// If there are values then populate relevent field with value.
document.addEventListener("DOMContentLoaded", function(event) {
var getStoredName = localStorage.name;
var getStoredAge = localStorage.age;
console.log(getStoredName ,getStoredAge);
if(getStoredName !==undefined || getStoredAge !== undefined){
document.getElementById("name").value=getStoredName;
document.getElementById("age").value=getStoredAge;
}
})
// Set local storage on click of next button
var getNextButton = document.getElementById("nextButton");
getNextButton.addEventListener("click",function(){
localStorage.name=document.getElementById("name").value;
localStorage.age=document.getElementById("age").value;
})
答案 1 :(得分:0)
想想你的问题: