[英]How to retain the entered data in an html form after clicking submit
I have a form in an html page that ends up being cleared after i click the submit button. 我在html页面中有一个表单,单击提交按钮后,该表单最终被清除。 I wanted to know how I to make the data stay in the form after clicking submit in case the user needs to fix any errors.
我想知道如何在单击“提交”后使数据保留在表单中,以防用户需要修复任何错误。 Any help would be appreciated!
任何帮助,将不胜感激!
Here is the html code: 这是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>
and here is the javascript code: 这是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;
}
Add return
with onclick onclick="return validate()"
使用onclick添加
return
onclick="return validate()"
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; } }
<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="return validate()" /> <input type="reset" value="Clear" /> </form>
First, add a submit handler to your form: 首先,在表单中添加一个提交处理程序:
<form id="contactform" action = "" onsubmit="handleSubmit()">
...
</form>
Then in your handler validate the input. 然后在您的处理程序中验证输入。 If its not valid you need to preventDefault() to stop the form from submitting.
如果无效,则需要阻止Default()停止提交表单。 Note: You'll have to
return true
at the end of validate()
if nothing is wrong. 注意:如果没有错误,则必须在
validate()
末尾return true
。 I don't see that in the question. 我没有在问题中看到这一点。
function handleSubmit(event) {
if(!validate()) {
event.preventDefault();
}
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.