在HTML表单不良做法上放置多个提交按钮? [英] Is putting multiple submit buttons on an HTML form bad practice?

查看:177
本文介绍了在HTML表单不良做法上放置多个提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个HTML多选题测验,并意识到一种技术可以让我使用多个提交按钮 - 一个用于针对单个问题的每个答案。然后,我可以使用提交按钮值在PHP中处理表单,并确定用户选择了哪个答案。使用提交按钮的原因是可以适当地设置风格。



然而,我想知道从可访问性的角度来看,这是否是不好的做法?对每个问题的答案使用单独的表单会更好吗?这里有很多关于如何使用多个提交按钮的问题,但他们似乎没有解决这一问题。

这绝对很好,并且在很多情况下可以提高表单的可用性。但请注意,因为有几个问题:


  1. 如果使用回车键提交表单,则提交行为未定义。 HTML5确实定义了这种行为,它指定了大多数浏览器在这种情况下已经做的事情:表单中的第一个提交按钮应该将其名称/值作为提交的一部分发送。

    然而,当输入键用于提交表单时,IE <= 8不会发送任何提交按钮的名称/值对。

    所以,你必须意识到需要对表单进行默认操作,并且必须是第一个提交元素。

  2. 您可以'不要使用这种技术根据按下哪个按钮来提交不同的操作。 Javascript理论上可以解决这个问题,但是你不应该那样做(一个好的口头禅,不要使用Javascript解决非Javascript问题



I'm building an HTML multiple-choice quiz and am aware of a technique which would let me use multiple submit buttons - one for each answer to an individual question. I could then process the form in PHP using submit button values and determine which answer the user has selected. The reason for using submit buttons is so that they can be styled appropriately.

However, I'm wondering if this is bad practice from an accessibility perspective? Would it be better to use an individual form for each answer to a question? There are plenty of questions on here about how to use multiple submit buttons but they don't seem to address this point.

解决方案

It's absolutely fine, and in a lot of cases can improve the usability of a form. Be careful however, as there are a couple of gotchas:

  1. If the enter key is used to submit the form, the submit behaviour is undefined. HTML5 does define this behaviour, and it specifies what most browsers already do in this situation: The first submit button in the form should have its name/value sent as part of the submission.

    However IE<=8 don't send the name/value pair for any submit button when the enter key is used to submit the form.

    So, you have to be aware that there needs to be a "default" action for the form, and that has to be the first submit element present.

  2. You can't use this technique to submit to a different action based on which button was pressed. Javascript can theoretically solve this, but you shouldn't do that (a good mantra is, don't use Javascript to solve a non-Javascript problem)

这篇关于在HTML表单不良做法上放置多个提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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