ブラウザでフォームに入力する際、ユーザーが Web フォーム上で複數(shù)の操作を?qū)g行できる柔軟性を提供することが重要です。リセットとして知られる特別なタイプのボタンの 1 つを使用すると、ユーザーが入力したフォームの値をリセットできます。このボタンにより、ユーザーはリセット操作を柔軟に実行できます。このボタンは基本的に、ユーザーが同じフォームに入力したデータをすべて消去します。リセット後、フォームにはデフォルト値が入力されます。この記事では、複數(shù)のシナリオでのリセット ボタンの使用法とその使用方法について説明します。
HTML リセット ボタンの構(gòu)文
リセット操作は で定義できます。タグまたは <ボタン>タグ。これを定義するためにこれらのタグで type 屬性が使用され、値「reset」が渡されます。
1.タグを入力
構(gòu)文:
<input type = "reset">
ここで、Input は、Input 要素を定義するために使用されるタグであり、type は、input 要素のタイプを定義するために使用される屬性です。値リセットは、この屬性に渡すことができる値の 1 つです。
2.ボタンタグ
構(gòu)文:
<button type = "reset">
リセットは、Button 要素の type 屬性に渡すことができる値の 1 つです。前述したように、これによりフォームがリセットされます。このタグでサポートされている他の 2 つの値は、「button」と「submit」です。
HTML リセット ボタンの実裝例
以下は HTML リセット ボタンの例です:
例 #1 – 入力タグを使用したリセット ボタン
コード:
<!DOCTYPE html>
<html>
<head>
<title>
Reset button in HTML
</title>
<style>
.form-data {
border : #81D4FA 2px solid;
background-color : #03a9f400;
text-align : left;
padding-left : 20px;
height : 450px;
width : 95%;
}
.form {
margin:5px auto;
max-width: 700px;
padding: 25px 15px 15px 25px;
}
.form li {
margin: 12px 0 0 0;
list-style: none;
}
.form label {
margin: 0 0 3px 0;
padding: 0px;
display: block;
font-weight: bold;
}
.form .field {
width: 80%;
height: 20px;
}
.form input[ type = submit], .form input[ type = reset] {
background: #2196F3;
padding: 10px 17px 10px 17px;
margin-right: 10px;
color: #fff;
border: none;
}
.form input[type = submit]:hover, .form input[ type = reset]:hover {
background: #2173f3;
}
.heading {
font-weight: bold;
border-bottom: 2px solid #ddd;
font-size: 15px;
width: 98%;
}
</style>
</head>
<body>
<div class = "form-data">
<div class = "heading">
<h2> HTML Reset Button </h2>
<p> Click on reset button to reset the form data. </p>
</div>
<div>
<form action = "#" >
<ul class = "form" >
<li>
<label> First Name <span class = "required"> * </span></label>
<input type = "text" name = "firstName" placeholder = " First name" class = "field"/>
</li>
<li>
<label> Last Name <span class = "required"> * </span></label>
<input type = "text" name = "lastName" placeholder = " Last name" class = "field" />
</li>
<li>
<label> Email <span class = "required" > * </span></label>
<input type="email" name="field3" class="field" />
</li>
<li>
<label> Message </label>
<textarea name = "message" id = "message" class = "field-message"></textarea>
</li>
<li>
<input type = "reset" value = "Reset">
<input type = "submit" value = "Submit" />
</li>
</ul>
</form>
</div>
</div>
<script type = "text/javascript">
</script>
</body>
</html>
出力:

入力ボックスに入力してみると、リセット ボタンを更新せずに入力したデータが消去されます。リセットを行う場合、ページを更新する必要はないことに注意してください。データは、読み込まれた同じページ上で動的にクリアされます。
コード:
<!DOCTYPE html>
<html>
<head>
<title>
Reset button in HTML
</title>
<style>
.form-data {
border : #81D4FA 2px solid;
background-color : #03a9f400;
text-align : left;
padding-left : 20px;
height : 450px;
width : 95%;
}
.form {
margin:5px auto;
max-width: 700px;
padding: 25px 15px 15px 25px;
}
.form li {
margin: 12px 0 0 0;
list-style: none;
}
.form label {
margin: 0 0 3px 0;
padding: 0px;
display: block;
font-weight: bold;
}
.form .field {
width: 80%;
height: 20px;
}
.form button[ type = submit], .form button[ type = reset] {
background: #2196F3;
padding: 10px 17px 10px 17px;
margin-right: 10px;
color: #fff;
border: none;
}
.form button[type = submit]:hover, .form button[ type = reset]:hover {
background: #2173f3;
}
.heading {
font-weight: bold;
border-bottom: 2px solid #ddd;
font-size: 15px;
width: 98%;
}
</style>
</head>
<body>
<div class = "form-data">
<div class = "heading">
<h2> HTML Reset Button </h2>
<p> Click on reset button to reset the form data. </p>
</div>
<div>
<form action = "#" >
<ul class = "form" >
<li>
<label> First Name <span class = "required"> * </span></label>
<input type = "text" name = "firstName" placeholder = " First name" class = "field"/>
</li>
<li>
<label> Last Name <span class = "required"> * </span></label>
<input type = "text" name = "lastName" placeholder = " Last name" class = "field" />
</li>
<li>
<label> Email <span class = "required" > * </span></label>
<input type="email" name="field3" class="field" />
</li>
<li>
<label> Message </label>
<textarea name = "message" id = "message" class = "field-message"></textarea>
</li>
<li>
<button type = "reset" value = "Reset"> Reset </button>
<button type = "submit" value = "Submit"> Submit </button>
</li>
</ul>
</form>
</div>
</div>
<script type = "text/javascript">
</script>
</body>
</html>
出力:

注: リセット ボタンは、フォーム要素內(nèi)に配置すると自動的に機(jī)能します。そのフォーム內(nèi)のリセット ボタンは、自動的にそれに関連付けられます。フォームタグの內(nèi)側(cè)に配置されたリセットボタンのみがそのフォームに適用され、外側(cè)からは適用されません。また、1 つのフォーム內(nèi)に複數(shù)のリセット ボタンを配置することができ、これらすべてのリセット ボタンが正しく動作します。ユーザーが誤ってリセット ボタンをクリックすると、入力したデータがすべて失われる可能性があるため、リセット ボタンは注意して使用することをお勧めします。
結(jié)論
HTML リセット ボタンは、Web ページを更新せずに、フォームに入力されたデータを自動的にリセットする強(qiáng)力な機(jī)能を提供します。リセット ボタンは通常、フォーム要素內(nèi)で使用されます。
以上がHTMLリセットボタンの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國語 Web サイトの他の関連記事を參照してください。