我在html頁面中有一個表單,在點擊提交按鈕後最終被清除。我想知道如何在點擊提交後將數據保留在表單中,以防用戶需要修復任何錯誤。任何幫助,將不勝感激!如何在點擊提交後在html表單中保留輸入的數據
下面是HTML代碼:
<form id = "contactform" action = "">
<label> Name:
<input name = "firstname" type = "text" id = "firstname" maxlength = "50"/>
</label>
<label> Last Name:
<input name = "lastname" type = "text" id = "lastname" maxlength = "150" />
</label>
<label> Address:
<input name = "address" type = "text" id = "address" maxlength = "200"/>
</label>
<label> Postcode:
<input name = "postcode" type = "text" id = "postcode" maxlength = "50" />
</label>
<input type = "submit" value = "Submit" onclick = "validate()" />
<input type = "reset" value = "Clear" />
</p>
</form>
,這裏是JavaScript代碼:
function validate() {
var firstname = document.getElementById('firstname');
var lastname = document.getElementById('lastname');
var address = document.getElementById('address');
var postcode = document.getElementById('postcode');
if(firstname.value == "") {
alert("Make sure the first name field is filled");
return false;
}
if(lastname.value == "") {
alert("Make sure the last name field is filled");
return false;
}
if(address.value == "") {
alert("Make sure the address field is filled");
return false;
}
if(postcode.value == "") {
alert("Make sure the post code field is filled");
return false;
}
爲什麼用戶想要編輯一些東西,他點擊了提交? – VivekN
^^因爲沒有人是完美的,可能忘記輸入他們的姓氏等等。隨後不想再填寫他的名字?只是一個預感。 – Zak
頁面刷新時會清除表單,您可以使用ajax,因此頁面不會刷新或在提交後將服務器端數據綁定到輸入字段。 – lilixiaocc