简体   繁体   English

单击提交后如何以html形式保留输入的数据

[英]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.

相关问题 单击按钮后如何将带有输入数据的TextField传递到表单? - How to pass TextField with entered data into form after clicking a button? 提交后保留在 html 表单中输入的数据,并通过 AJAX 保存数据 - Keep data entered on html form after submit with data saved via AJAX 单击提交后添加表单数据 - Appending form data after clicking submit 单击提交按钮后如何在不通过服务器的情况下如何从HTML页面的形式获取数据到另一个页面 - How to get data from a form of an Html page to another page after clicking submit button, without going through the Server 通过jQuery提交表单后如何保留表单值? - How to retain form values after form submit through jquery? 如何在用户输入数据后 2 秒自动提交(不单击提交按钮)表单 - How to auto-submit(without clicking submit button) the form , 2 seconds after the user has typed in the data 如何在laravel刀片中提交表单后保留年,月的值 - how to retain values in year,month after form submit in laravel blade 单击提交按钮后,用户输入的详细信息必须显示在另一个注册表格中 - After clicking submit button the details entered by user has to be shown in another registration form 如何在多次点击提交后只提交一次表单? - How to submit form only once after multiple clicking on submit? 单击html表单后使用php提交后弹出 - popup after clicking the html form submit using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM