最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How does form.reset() work? - Stack Overflow

programmeradmin9浏览0评论

I know that form.reset() will reset all form fields to their default values, but how does that work?

Is it the browser's DOM implementation? i.e. the browser knows what were the last values sent from the server in last postback/get and when reset() is called the browser resets those values.

I know that form.reset() will reset all form fields to their default values, but how does that work?

Is it the browser's DOM implementation? i.e. the browser knows what were the last values sent from the server in last postback/get and when reset() is called the browser resets those values.

Share Improve this question edited Oct 31, 2013 at 4:45 icedwater 4,8873 gold badges38 silver badges53 bronze badges asked Nov 8, 2010 at 15:29 TiagoDiasTiagoDias 1,8652 gold badges17 silver badges22 bronze badges 5
  • 1 The way I've always understood it is that .reset() will return the fields in the form to the values that the browser first saw when the page was loaded. But you already know that. Not sure how it works. I wonder if there is a way in JS to be able to read what was the first value on page load? That way you could reset individual fields – Scott Commented Nov 8, 2010 at 15:34
  • 4 @Brady there's the element.defaultValue attribute for that – Pekka Commented Nov 8, 2010 at 15:36
  • @Pekka nice, didn't know that. Is that reliably available cross-browser? – Sean Patrick Floyd Commented Nov 8, 2010 at 15:39
  • @Pekka - +1 for that little tidbit I didn't yet know ;) – Scott Commented Nov 8, 2010 at 15:41
  • @seanizer yup, it's defined in DOM level 1: msdn.microsoft.com/en-us/library/ms533718(VS.85).aspx – Pekka Commented Nov 8, 2010 at 15:44
Add a comment  | 

2 Answers 2

Reset to default 9

The DOM spec tells us that it:

...performs the same action as a reset button.

And so off to HTML. According to the latest spec (still a draft!):

When a form element form is reset, the user agent must fire a simple event named reset, that is cancelable, at form, and then, if that event is not canceled, must invoke the reset algorithm of each resettable element whose form owner is form, and broadcast formchange events from form.

Each resettable element defines its own reset algorithm. Changes made to form controls as part of these algorithms do not count as changes caused by the user (and thus, e.g., do not cause input events to fire).

And the "reset algorithm" for input elements (for instance) is:

...to set the dirty value flag and dirty checkedness flag back to false, set the value of the element to the value of the value content attribute, if there is one, or the empty string otherwise, set the checkedness of the element to true if the element has a checked content attribute and false if it does not, empty the list of selected files, and then invoke the value sanitization algorithm, if the type attribute's current state defines one.

So basically, reset sets the value of an input to the current value of its "value" attribute (theElement.getAttribute("value")), which may be different from its current value property (theElement.value). Live example here.

Edit: Oooh, Pekka points us at defaultValue. Very cool, I'd much rather use that than getAttribute("value"). Revised live example.

reset will use the values in the current HTML source code. The Browser does not know anything about the last Request in this Request (that would be a huge security breach).

If the form contains these elements:

<form action="xyz">
    <input type="text" name="fieldA" />
    <input type="text" name="fieldB" value="Thingy" />
    <input type="reset" value="Click me to undo everything" />
    <input type="button" onclick="this.form.reset()" value="Click me too" />
</form>

Both buttons will empty the first field and set the value of the second field to Thingy.

发布评论

评论列表(0)

  1. 暂无评论