在瀏覽器中填寫表單時(shí),為用戶提供在 Web 表單上執(zhí)行多項(xiàng)操作的靈活性非常重要。一種稱為重置的特殊類型按鈕允許重置用戶輸入的表單中的值。該按鈕為用戶提供了執(zhí)行重置操作的靈活性。該按鈕基本上會(huì)清除用戶在同一表單上輸入的所有數(shù)據(jù)。重置后,表單將填充默認(rèn)值。本文將介紹重置按鈕在多種場景下的使用方法以及使用方法。
HTML 重置按鈕的語法
重置操作可以在上定義標(biāo)簽或 標(biāo)簽。這些標(biāo)簽上使用 type 屬性來定義它,并將值“reset”傳遞給它。
1.輸入標(biāo)簽
語法:
<input type = "reset">
這里,Input是用于定義Input元素的標(biāo)簽,type是用于定義輸入元素類型的屬性。重置值是可以傳遞給此屬性的值之一。
2.按鈕標(biāo)簽
語法:
<button type = "reset">
重置是可以傳遞給 Button 元素的 type 屬性的值之一。如前所述,這將重置表單。該標(biāo)簽支持的另外兩個(gè)值是“button”和“submit”。
實(shí)現(xiàn) HTML 重置按鈕的示例
以下是 HTML 重置按鈕的示例:
示例 #1 – 使用輸入標(biāo)簽的重置按鈕
代碼:
<!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>
輸出:

嘗試在輸入框中鍵入內(nèi)容,如果不刷新單擊重置按鈕,輸入的數(shù)據(jù)將被擦除。請注意,為了進(jìn)行重置,我們不需要刷新頁面;數(shù)據(jù)將在同一加載頁面上動(dòng)態(tài)清除。
示例 #2 – 使用按鈕標(biāo)簽重置按鈕
代碼:
<!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)時(shí)會(huì)自動(dòng)工作。該表單內(nèi)的重置按鈕會(huì)自動(dòng)與其關(guān)聯(lián)。只有放置在表單標(biāo)簽內(nèi)部的重置按鈕才適用于該表單,而不是外部的重置按鈕。另外,一個(gè)表單內(nèi)可以放置多個(gè)重置按鈕,并且這些重置按鈕都將正常工作。建議謹(jǐn)慎使用重置按鈕,因?yàn)橛脩粲锌赡苠e(cuò)誤地單擊重置按鈕,并且用戶將丟失所有輸入的數(shù)據(jù)。
結(jié)論
HTML重置按鈕提供了強(qiáng)大的功能,可以自動(dòng)重置表單中輸入的數(shù)據(jù),而無需刷新網(wǎng)頁。重置按鈕通常在表單元素內(nèi)使用。
以上是HTML 重置按鈕的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!