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

发布于 2024-08-22 21:23:54 字数 654 浏览 5 评论 0原文

在一个页面上有多个 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>

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

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

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

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.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终弃我 2024-08-29 21:23:54

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

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

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.

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

指尖微凉心微凉 2024-08-29 21:23:54

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

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

青丝拂面 2024-08-29 21:23:54

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文