Html 具有相同名称属性的输入字段的多个表单?是好是坏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2292384/
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 02:14:24  来源:igfitidea点击:

Multiple Forms With Input Fields With The Same Name Attribute? Good Or Bad?

htmlxhtmlaccessibilityweb-standards

提问by Richard Knop

Is it an acceptable practice to have multiple HTML forms on a page with input fields that share the same name attribute? For example, the page contains a listing of all players and users are allowed to vote for the best player so next to each player card there is this form:

在具有共享相同名称属性的输入字段的页面上具有多个 HTML 表单是否可以接受?例如,该页面包含所有球员的列表,并且允许用户投票选出最佳球员,因此在每张球员卡旁边都有以下表格:

<form class="vote-for-player" enctype="application/x-www-form-urlencoded" method="post" action="/index/vote-for-best-player">
    <input type="hidden" name="player_id" value="1" />
    <input type="submit" name="vote_for_player" value="Vote" class="input-submit" />
</form>

Value attribute of the hidden input field is different for each form, of course.

当然,每个表单的隐藏输入字段的值属性是不同的。

Let's say there are 20 forms like this on the page so that means 20 input fields with the name equal to "player_id". If I pass that page through HTML validator, it is valid even with the XHTML 1.0 Strict doctype. But is this an acceptable practice from web standards or accessibility perspective?

假设页面上有 20 个这样的表单,这意味着 20 个输入字段的名称等于“player_id”。如果我通过 HTML 验证器传递该页面,即使使用 XHTML 1.0 Strict 文档类型,它也是有效的。但从 Web 标准或可访问性的角度来看,这是一种可接受的做法吗?

One thing I know for sure, it makes the server side processing of the page easier as I just need to load value from one POST field called player_id.

我确定知道的一件事是,它使页面的服务器端处理更容易,因为我只需要从一个名为 player_id 的 POST 字段加载值。

采纳答案by Greg

Agree with above answer. Name is totally ok, and will be passed as response parameter of your form. Different story would be if your input elements would have same id's as well - some browsers might have problems traversing dom of your document.

同意楼上的回答。名称完全可以,并将作为表单的响应参数传递。如果您的输入元素也具有相同的 id,情况就会有所不同——某些浏览器可能会在遍历文档的 dom 时遇到问题。

Again, think of bunch of radio buttons, where users can select gender etc. They must have same name (but different id's)...

再次,想想一堆单选按钮,用户可以在其中选择性别等。它们必须具有相同的名称(但 ID 不同)...

回答by cherouvim

Yes it is valid, I do it all the time.

是的,这是有效的,我一直这样做。

回答by svg

yes that's perfectly fine.. in fact i personally feel that its really a good practice to do so as it turns relatively pretty handy for developers to work with relatively less names as compared to bunch of them.. moreover when elements are on different forms it dose not cause name space collision in any way since to identify elements in js by name we use both form name as well as input field name so it dose not cause any issues...

是的,这完全没问题..事实上,我个人认为这样做确实是一个很好的做法,因为与一堆名称相比,开发人员使用相对较少的名称变得相对非常方便..此外,当元素采用不同的形式时不会以任何方式导致名称空间冲突,因为通过名称识别 js 中的元素我们同时使用表单名称和输入字段名称,因此它不会导致任何问题......