可以有多个同名的 HTML 表单吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11111670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:09:10  来源:igfitidea点击:

Is it OK to have multiple HTML forms with the same name?

htmlforms

提问by Nate

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?

这是可以接受的吗?

回答by Jukka K. Korpela

Regarding the HTML 4.01 specication, you can use formelements with the same nameattribute, 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.foorefers to that form.

关于 HTML 4.01 规范,您可以使用form具有相同name属性的元素,因为对它们没有唯一性要求。但是,这样做违背了这些属性的目的。它们旨在使在客户端脚本中更容易引用表单:如果您有<form name=foo>,则document.foo引用该表单。

It is undefined what happens when same nameattribute is used, but what browsers seem to do is to return an array. In your example, document.foowould 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.

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

The nameattribute itself is outdated for formelements (but not for form fields, where it keeps being essential). The HTML 4.01 spec clause on formsays:

name属性本身是过时的form元素(但不包括表单字段,它一直是必不可少的)。在对HTML 4.01规范条款form说:

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 idattribute to identify elements.”

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

In the HTML5 drafts, even the formal rules disallow the use of the same nameattribute. The HTML5 clause on the nameattribute on formsays that its value “must be unique amongst the formelements in the formscollection that it is in, if any”. This is a confusing formulation, but it is safest to assume that it must be unique within the formelements of a document.

在 HTML5 草案中,即使是正式规则也不允许使用相同的name属性。关于 onname属性formHTML5 子句说它的值“formforms它所在的集合中的元素中必须是唯一的,如果有的话”。这是一个令人困惑的表述,但最安全的做法是假设它在form文档的元素中必须是唯一的。

回答by secretformula

Yes it is allowed, only id's must be unique. I wouldn't recommend it however, why even put yourself in a position to be confused down the road.

是的,这是允许的,只有id's 必须是唯一的。然而,我不会推荐它,为什么甚至让自己陷入迷茫的境地。

The nameattribute only defines what each form field element will be represented as when sent to the server.

name属性仅定义了每个表单字段元素在发送到服务器时将被表示的内容。

回答by Rafael Kutscha

It is also ok in HTML5. Only the name must be unique inside the form itself.

在 HTML5 中也可以。只有名称在表单本身内必须是唯一的。

See the docs: "The value must not be the empty string, and the value must be unique amongst the form elements in the forms collection that it is in, if any."

请参阅文档:“该值不得为空字符串,并且该值在它所在的表单集合中的表单元素中必须是唯一的(如果有)。”

回答by Erenor Paz

When the user clicks a submit button, only that form will be taken in action. Still, it should be better to name them so that you are not confused :)

当用户单击提交按钮时,只有该表单会被执行。不过,最好为它们命名,以免混淆:)