<input type="reset"> - HTML: HyperText Markup Language 编辑

<input> elements of type reset are rendered as buttons, with a default click event handler that resets all of the inputs in the form to their initial values.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

You should usually avoid including reset buttons in your forms. They're rarely useful, and are instead more likely to frustrate users who click them by mistake (often while trying to click the submit button).

ValueA DOMString used as the button's label
Eventsclick
Supported common attributestype and value
IDL attributesvalue
MethodsNone

Value

An <input type="reset"> element's value attribute contains a DOMString that is used as the button's label. Buttons such as reset don't have a value otherwise.

<input type="reset" value="Reset the form">

If you don't specify a value, you get an button with the default label (typically "Reset," but this will vary depending on the user agent):

<input type="reset">

Using reset buttons

<input type="reset"> buttons are used to reset forms. If you want to create a custom button and then customize the behaviour using JavaScript, you need to use <input type="button">, or better still, a <button> element.

A simple reset button

We'll begin by creating a simple reset button:

<form>
  <div>
    <label for="example">Type in some sample text</label>
    <input id="example" type="text">
  </div>
  <div>
    <input type="reset" value="Reset the form">
  </div>
</form>

This renders like so:

Try entering some text into the text field, and then pressing the reset button.

Adding a reset keyboard shortcut

To add a keyboard shortcut to a reset button — just as you would with any <input> for which it makes sense — you use the accesskey global attribute.

In this example, r is specified as the access key (you'll need to press r plus the particular modifier keys for your browser/OS combination; see accesskey for a useful list of those).

<form>
  <div>
    <label for="example">Type in some sample text</label>
    <input id="example" type="text">
  </div>
  <div>
    <input type="reset" value="Reset the form"
     accesskey="r">
  </div>
</form>

The problem with the above example is that there's no way for the user to know what the access key is! This is especially true since the modifiers are typically non-standard to avoid conflicts. When building a site, be sure to provide this information in a way that doesn't interfere with the site design (for example by providing an easily accessible link that points to information on what the site access keys are). Adding a tooltip to the button (using the title attribute) can also help, although it's not a complete solution for accessibility purposes.

Disabling and enabling a reset button

To disable a reset button, specify the disabled global attribute on it, like so:

<input type="reset" value="Disabled" disabled>

You can enable and disable buttons at run time by setting disabled to true or false; in JavaScript this looks like btn.disabled = true or btn.disabled = false.

Note: See the <input type="button"> page for more ideas about enabling and disabling buttons.

Validation

Buttons don't participate in constraint validation; they have no real value to be constrained.

Examples

We've included simple examples above. There isn't really anything more to say about reset buttons. 

Specifications

SpecificationStatus
HTML Living Standard
The definition of '<input type="reset">' in that specification.
Living Standard
HTML5
The definition of '<input type="reset">' in that specification.
Recommendation

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:99 次

字数:10533

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文