具有相同名称属性的输入字段的多个表单?是好是坏? [英] Multiple Forms With Input Fields With The Same Name Attribute? Good Or Bad?

查看:43
本文介绍了具有相同名称属性的输入字段的多个表单?是好是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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?

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

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.

推荐答案

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

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 不同)...

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

这篇关于具有相同名称属性的输入字段的多个表单?是好是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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