javascript - 作为表单属性的表单元素

标签 javascript dom

如果您在表单中有一个表单元素,并且该元素有一个 nameid,您可以直接从表单的 DOM 元素中访问它作为一个属性姓名。这种行为似乎得到了非常广泛的支持。它是否包含在任何规范中,如果是,是哪一个?

表单的 elements 集合的这种行为 is specified在 DOM2 HTML 规范中,但我没有立即看到将元素作为属性转储到表单本身指定的任何地方的业务,除非短语 “它提供对包含的表单控件以及属性的直接访问表单元素。” here意思是说(如果是这样,哇是那么微妙,我读它是指 elements 集合)。我猜这只是遗留行为。

为清楚起见:我不是在问访问表单字段的最佳方式是什么,我是在问这种行为是否包含在标准中。 (我也避免了将所有事情转储到 window 对象上;那是另一个话题。)

示例(live copy):

HTML:

<form id="theForm">
<input type="text" name="field1" value="foo">
<input type="text" id="field2" value="bar">
</form>

JavaScript:

var f = document.getElementById("theForm");
console.log(f.elements.field1.value); // "foo", per spec
console.log(f.field1.value);          // also "foo"
console.log(f.elements.field2.value); // "bar", per spec
console.log(f.field2.value);          // also "bar"

我检查过 IE6、7、8 和 9、Firefox 4.0、Firefox 3.6、Chrome 12、Opera 11 和 Safari 5。它们都这样做(使表单字段在两个地方都可用)。

最佳答案

找到了 in the draft HTML5 specification :

When a form element is indexed for indexed property retrieval, the user agent must return the value returned by the item method on the elements collection, when invoked with the given index as its argument.

Each form element has a mapping of names to elements called the past names map. It is used to persist names of controls even when they change names.

The supported property names are the union of the names currently supported by the object returned by the elements attribute, and the names currently in the past names map.

When a form element is indexed for named property retrieval, the user agent must run the following steps:

  1. If name is one of the supported property names of the object returned by the elements attribute, then run these substeps:

    1. Let candidate be the object returned by the namedItem() method on the object returned by the elements attribute when passed the name argument.

    2. If candidate is an element, then add a mapping from name to candidate in the form element's past names map, replacing the previous entry with the same name, if any.

    3. Return candidate and abort these steps.

  2. Otherwise, name is the name of one of the entries in the form element's past names map: return the object associated with name in that map.

If an element listed in the form element's past names map is removed from the Document, then its entries must be removed from the map.

...我们从 DOM2 HTML specification's section 中知道在 ECMAScript 语言映射上,通过 [] 进行索引最终会调用 item(对于数字)或 namedItem(对于字符串)。

感谢@Carey his answer to my other related question ,这让我面面相觑并查看了 HTML5 草案,因为他们试图将 HTML DOM 内容纳入规范。

关于javascript - 作为表单属性的表单元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6503764/

相关文章:

Javascript 字符串到嵌套数组

javascript - Facebook FB.logout() 是否还有对话框?

javascript - 为什么 document.body.offsetHeight + document.body.bottomMargin 不等于 document.documentElement.offsetHeight

javascript - typescript Canvas

javascript - 如何在文档准备好之前强制从 js 准备好边距

javascript - 样式表更新?

javascript - 可以附加很多事件监听器吗?

javascript - 为什么jQuery或诸如getElementById之类的DOM方法找不到元素?

表单提交后 jQuery.hide() 不工作

javascript - react 组件不呈现来自 redux 存储的新数据