form.reset()如何工作? [英] How does form.reset() work?

查看:114
本文介绍了form.reset()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道, form.reset()会将所有表单字段重置为默认值,但是如何工作?

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

是浏览器的DOM实现吗?即浏览器知道在最后回发/获取时从服务器发送的最后一个值是什么,当 reset()被称为浏览器重置这些值时。

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.

推荐答案

DOM规范告诉我们


。执行与重置按钮相同的操作。

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

等等。根据最新规范(仍然是一个草稿!):

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


当表单元素复位时,用户代理必须触发一个名为reset的简单事件,这是可以取消的,在形式上,然后,如果事件没有被取消,则必须调用形式所有者形成的每个可重置元素的重置算法,并从表单广播formchange事件。

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:


...将脏值标志和脏检查标志设置为false,将元素的值设置为值content属性的值,如果有的话,否则为空字符串,则将元素的检查度设置为如果元素具有检查的内容属性,则为true,如果元素没有,则为false,如果没有,则为空,将所选文件的列表清空,然后调用值清理算法,如果类型属性的当前状态定义一个。

...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.

所以基本上, reset 将输入的值设置为其值属性的当前值( theElement.getAttribute(value)),可能与目前的值不同 属性 theElement.value )。 在这里的实例

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.

编辑:Oooh,Pekka指出我们在 默认值 。非常酷,我宁愿使用比 getAttribute(value)修改实例。

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

这篇关于form.reset()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆