2017-06-06 3 views
0

HTMLページに、送信ボタンをクリックすると消去されるフォームがあります。ユーザーがエラーを修正する必要がある場合に備えて、submitをクリックした後にデータをフォームに残す方法を知りたかったのです。どんな助けもありがとう!ここで投稿をクリックした後に入力したデータを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; 
     } 
+0

なぜ送信をクリックしたユーザーは何かを編集したいのですか? – VivekN

+0

^^誰も完璧ではなく、おそらく姓を入力するのを忘れてしまったからです。彼の名前をもう一度記入したくないのですか? .. – Zak

+0

ページがリフレッシュされるときにフォームが消去され、ajaxを使用できるため、ページがリフレッシュされず、送信後に入力フィールドにサーバーの終了データがバインドされます。 – lilixiaocc

答えて

0

onclickの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>

returnを追加します
+0

ありがとうございました! – rajzaveri5

+0

あなたは@ rajzaveri5を歓迎します。私の答えは有用です[回答としてマーク](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved = 0ahUKEwif_KTAjKvUAhXMtY8KHZFLCY4QFggpMAE&URL = httpsの%3A%2F%2Fstackoverflow.com%2Fhelp%2Fsomeone-解答&USG = AFQjCNHZ4IpbyGACPNwGrF1LYJUvBvZJGw&SIG2 = hPdlhiSZ2L321-uLvjDX3A)将来の訪問者 – prasanth

0

まず、自分のフォームに提出するハンドラを追加します。

<form id="contactform" action = "" onsubmit="handleSubmit()"> 
    ... 
</form> 

次に、あなたのハンドラ内で入力を検証。有効でない場合は、フォームの送信を停止するためにDefault()を禁止する必要があります。 注:何か問題がなければ、validate()の末尾にreturn trueが必要です。私はその質問にそれを見ない。

function handleSubmit(event) { 
    if(!validate()) { 
    event.preventDefault(); 
    } 
    return; 
} 
+0

ために、より便利.Itsあなたの助けをありがとうございました! – rajzaveri5

関連する問題