Reset button (input type="reset")
Basic Concept of Reset Button
The <input type="reset">
is a special button type in HTML forms. When clicked, it resets all form input fields to their initial values. These initial values can be default values set via the value
attribute in HTML, or fields will be cleared if no default value is specified.
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="Default User">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<input type="reset" value="Reset Form">
<input type="submit" value="Submit">
</form>
In this example, clicking the "Reset Form" button will restore the username field to "Default User" and clear the email field.
Attributes of Reset Button
The reset button supports all standard <input>
element attributes, plus some specific ones:
- value: Defines the text displayed on the button
- disabled: Disables the reset button
- form: Specifies which form the button belongs to (used when the button is outside a
<form>
)
<form id="myForm">
<!-- Form content -->
</form>
<input type="reset" value="Reset" form="myForm" disabled>
Behavioral Characteristics of Reset Button
Reset buttons have several important behavioral traits to note:
- They don't trigger form submission
- They only reset elements within the same form
- They reset to initial values, not empty values
- They don't trigger
onchange
events
<form>
<input type="text" value="Initial Value" onchange="console.log('Value changed')">
<input type="reset" value="Reset">
</form>
In this example, after modifying the text box and clicking reset, the console won't output "Value changed".
Reset Button and JavaScript Interaction
While reset buttons are simple, their functionality can be enhanced with JavaScript:
<form id="userForm">
<input type="text" name="username" value="">
<input type="reset" value="Reset" onclick="confirmReset()">
</form>
<script>
function confirmReset() {
if(confirm("Are you sure you want to reset the form?")) {
document.getElementById("userForm").reset();
}
}
</script>
Styling Customization of Reset Button
Reset buttons can be styled with CSS like other buttons:
<style>
.custom-reset {
background-color: #ff6347;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.custom-reset:hover {
background-color: #ff4500;
}
</style>
<form>
<input type="reset" class="custom-reset" value="Clear All">
</form>
Alternative Solutions to Reset Button
In some cases, developers might choose JavaScript implementations instead of native reset buttons:
<form id="altForm">
<input type="text" name="search" value="Search...">
<button type="button" onclick="resetForm()">Clear</button>
</form>
<script>
function resetForm() {
document.getElementById("altForm").reset();
// Additional reset logic can be added here
}
</script>
Usability Considerations for Reset Button
When using reset buttons, consider user experience:
- Button labels should clearly indicate function ("Reset Form" is better than just "Reset")
- Consider adding confirmation dialogs to prevent accidental actions
- Reset functionality is more valuable in long forms
- Ensure proper focus management after reset
<form>
<!-- Multiple form fields -->
<input type="reset" value="Reset Form"
aria-label="Reset all form fields to initial values"
onclick="return confirm('This will clear all inputs. Confirm?')">
</form>
Reset Button and Form Validation
Reset buttons bypass form validation and directly reset all fields:
<form>
<input type="text" required>
<input type="reset" value="Reset">
<input type="submit" value="Submit">
</form>
Even if required fields are empty, the reset button will still work while the submit button will be blocked.
Browser Compatibility of Reset Button
<input type="reset">
has excellent support across all modern browsers, including:
- Chrome
- Firefox
- Safari
- Edge
- Opera
- Internet Explorer (including older versions)
Advanced Usage of Reset Button
More complex reset logic can be implemented with JavaScript:
<form id="advForm">
<input type="text" name="field1" class="preserve-on-reset">
<input type="text" name="field2">
<input type="reset" value="Smart Reset" onclick="smartReset(event)">
</form>
<script>
function smartReset(e) {
e.preventDefault();
const form = document.getElementById("advForm");
const fields = form.elements;
for(let i = 0; i < fields.length; i++) {
if(!fields[i].classList.contains("preserve-on-reset")) {
fields[i].value = fields[i].defaultValue;
}
}
}
</script>
Using Reset Button in Frameworks
In modern frontend frameworks, reset button behavior may differ from native HTML:
React Example:
function FormComponent() {
const [formData, setFormData] = useState({
name: '',
email: ''
});
const handleReset = () => {
setFormData({
name: '',
email: ''
});
};
return (
<form>
<input
type="text"
value={formData.name}
onChange={(e) => setFormData({...formData, name: e.target.value})}
/>
<button type="button" onClick={handleReset}>Reset</button>
</form>
);
}
Best Practices for Reset Button
- Use reset buttons cautiously, especially in forms with extensive user input
- Consider "clear" functionality instead of full reset
- Provide clear visual feedback
- Ensure proper button size on mobile devices
- Consider using icons with text for better recognition
<form>
<input type="text" name="query" placeholder="Search...">
<button type="reset" class="icon-button">
<svg width="16" height="16" viewBox="0 0 24 24">...</svg>
<span>Clear Search</span>
</button>
</form>
Reset Button and Accessibility
Ensure reset buttons are accessible to all users:
<form>
<label for="search">Search:</label>
<input type="text" id="search" name="search">
<input type="reset" id="resetBtn" value="Clear Search">
<!-- Additional instructions for screen readers -->
<div id="resetHelp" class="visually-hidden">
This button will clear all search criteria
</div>
</form>
<script>
document.getElementById("resetBtn").setAttribute("aria-describedby", "resetHelp");
</script>
Testing Considerations for Reset Button
When testing reset buttons, verify:
- Whether all fields restore to initial values
- Proper focus management
- Interaction with form validation
- Behavior after dynamically added fields
- Interaction with browser autofill
<form id="testForm">
<input type="text" name="test1" value="Default1">
<input type="text" name="test2" value="Default2">
<input type="reset" id="testReset" value="Test Reset">
</form>
<script>
// Test code example
const form = document.getElementById("testForm");
const resetBtn = document.getElementById("testReset");
// Modify field value
form.test1.value = "Modified Value";
// Simulate click
resetBtn.click();
// Verify value restoration
console.assert(form.test1.value === "Default1", "Reset function failed");
</script>
Historical Evolution of Reset Button
The <input type="reset">
has existed since HTML 2.0 specification (1995) and its behavior has remained largely consistent. Its usage has declined with web development progress due to:
- Rise of modern Single Page Applications (SPA)
- Need for more granular form control
- Advances in user experience research
Nevertheless, it still holds value in traditional web applications.
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn