表单元素作为窗体的属性 [英] Form elements as properties of the form

查看:129
本文介绍了表单元素作为窗体的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在表单中有表单元素,该元素具有名称 id ,则可以访问它直接从窗体的DOM元素作为具有该名称的属性。这种行为似乎是非常广泛的支持。是否覆盖任何规范,如果是,哪一个?

If you have a form element within a form and that element has a name or id, you can access it directly off the form's DOM element as a property with that name. This behavior appears to be very broadly-supported. Is it covered by any specification, and if so, which one?

表单的元素集合<在DOM2 HTML规范中指定了href =http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-76728479 =nofollow> ,但是我没有立即看到将元素作为属性放置在表单本身指定的任何地方的业务,除非短语它可以直接访问包含的表单控件以及表单元素的属性。 here 旨在说(如果是这样,哇是微妙的,我把它看作是指元素集合)。我猜这只是遗留的行为。

This behavior for a form's elements collection is specified in the DOM2 HTML spec, but I'm not immediately seeing the business of dumping the elements as properties on the form itself specified anywhere, unless the phrase "It provides direct access to the contained form controls as well as the attributes of the form element." here is meant to say that (if so, wow is that subtle, I read it as referring to the elements collection). I'm guessing it's just legacy behavior.

为了清楚起见,我不是问什么最好的方式来访问表单域是,我问这个行为是否是被标准覆盖。 (我也是避免把所有的事情转移到窗口对象之间,这是一个其他的话题。)

For clarity: I'm not asking what the best way to access form fields is, I'm asking if this behavior is covered by a standard. (I'm also avoiding the whole business of things being dumped on the window object; that's a whole other topic.)

示例(直播副本):

HTML :

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

JavaScript:

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.他们都这样做(使表单字段在两个地方都可用)。

I've checked IE6, 7, 8, and 9, Firefox 4.0, Firefox 3.6, Chrome 12, Opera 11, and Safari 5. They all do it (make the form fields available in both places).

推荐答案

在HTML5规范草案中找到

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

受支持的属性名称是当前由<$ c返回的对象支持的名称的并集$ c>元素属性,以及当前名称中的名称映射。

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. 如果名称元素属性返回的对象的受支持属性名称之一,则运行以下子步骤:

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


  1. 候选人成为 namedItem()返回的对象

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

如果候选人是一个元素,则在表单中添加从名称候选 / code>元素的p ast姓名地图,用相同的名称替换以前的条目,如果有的话。

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.

返回候选人并中止这些步骤。 p>

Return candidate and abort these steps.


  • 否则,名称 form 元素的过去名称映射:返回与该地图中与 name 相关联的对象。

  • 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规范的部分关于ECMAScript语言映射的索引通过 [] 最后是调用项目(数字)或 namedItem (用于字符串)。

    ...and we know from the DOM2 HTML specification's section on ECMAScript language mapping that indexing via [] ends up being a call to either item (for numbers) or namedItem (for strings).

    感谢@Carey for 他对我的其他相关问题的回答,这使我看起来很好看HTML5草案,因为他们试图将HTML DOM东西纳入规范。

    Thanks to @Carey for his answer to my other related question, which made me facepalm and look in the HTML5 draft, since they're trying to subsume the HTML DOM stuff into the spec.

    这篇关于表单元素作为窗体的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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