有多个具有相同名称的HTML表单可以吗? [英] Is it OK to have multiple HTML forms with the same name?

查看:358
本文介绍了有多个具有相同名称的HTML表单可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要做到这一点的正当理由,但是这是一个漫长的故事,所以我会忘记试图解释为什么,只是问是否可以做。



我有一个页面,我需要使用同一个名称的多个表单,但我只想要提交其提交按钮的表单。例如,以下内容可能在我的页面上:

 < form name =inputaction =method =得到> 
用户名:< input type =textname =user/>
< input type =submitvalue =Submit/>
< / form>

text

用户名:< input type =textname =user/>
< input type =submitvalue =Submit/>
< / form>

text

用户名:< input type =textname =user/>
< input type =submitvalue =Submit/>
< / form>

这可以接受吗?

解决方案

关于HTML 4.01规范,您可以使用具有相同名称属性的 form 元素,因为它们没有唯一性要求。但是,这样做会破坏这些属性的目的。它们是为了更容易引用客户端脚本中的表单:如果您有< form name = foo> ,那么文档。



name 属性相同时会发生什么被使用,但是浏览器似乎做的是返回一个数组。在你的例子中, document.foo 是一个3元素数组,其中 document.foo [0] 是第一种形式。但是这是没有用的,因为(假设文档中没有其他的形式),你可以使用 document.forms [0] ,并且有明确的含义。 p>

名称属性本身对于形式元素已过时而不是表单域,它一直是必不可少的)。 格式的 HTML 4.01 spec条款 表示:



name = cdata [CI]
此属性将元素命名为元素以便它可以从样式表或脚本中引用。注意。包含此属性是为了向后兼容。应用程序应该使用 id 属性来标识元素。



在HTML5草案中,即使正式的规则也不允许使用相同的名称属性。 名称上的 HTML5子句表单 中的属性表示它的值必须在表单 中的元素形成集合,如果有的话。这是一个令人困惑的表述,但假设它必须在文档的表单元素中是唯一的。


I have a valid reason for wanting to do this, but it's a long story so I'll forgot trying to explain why and just ask if it's OK to do.

I have a page where I need to have multiple forms with the same name, but I only want the form whose submit button is click to be submitted. For example, the following might be on my page:

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

text

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

text

<form name="input" action="" method="get">
Username: <input type="text" name="user" />
<input type="submit" value="Submit" />
</form>

Is this acceptable?

解决方案

Regarding the HTML 4.01 specication, you can use form elements with the same name attribute, as there is no uniqueness requirement on them. Doing so defeats the purpose of such attributes, though. They are meant for making it easier to refer to forms in client-side scripting: if you have <form name=foo>, then document.foo refers to that form.

It is undefined what happens when same name attribute is used, but what browsers seem to do is to return an array. In your example, document.foo would be a 3-element array, with document.foo[0] being the first form. But this is not useful, since (assuming there are no other forms in the document) you could use document.forms[0], with a well-defined meaning.

The name attribute itself is outdated for form elements (but not for form fields, where it keeps being essential). The HTML 4.01 spec clause on form says:

"name = cdata [CI] This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements."

In the HTML5 drafts, even the formal rules disallow the use of the same name attribute. The HTML5 clause on the name attribute on form says that its value "must be unique amongst the form elements in the forms collection that it is in, if any". This is a confusing formulation, but it is safest to assume that it must be unique within the form elements of a document.

这篇关于有多个具有相同名称的HTML表单可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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