HTML 表单上的多个提交按钮指定一个按钮为默认按钮
我有一个包含三个提交按钮的表单,如下所示:
<input type="submit" name="COMMAND" value="‹ Prev">
<input type="submit" name="COMMAND" value="Save">
<input type="reset" name="NOTHING" value="Reset">
<input type="submit" name="COMMAND" value="Next ›">
<input type="button" name="NOTHING" value="Skip ›" onclick="location = 'yada-yada.asp';">
按钮行是提交、重置和 JavaScript 按钮的组合。按钮的顺序可能会发生变化,但无论如何,保存按钮仍位于上一个按钮和下一个按钮之间。
这里的问题是,当用户点击 Enter 提交表单时,post 变量“COMMAND”包含“Prev”;正常,因为这是表单上的第一个提交按钮。然而,我希望当用户通过 Enter 按钮提交表单时触发“下一步”按钮。这有点像将其设置为默认提交按钮,即使在它之前还有其他按钮。
I have a form that has three submit buttons as follows:
<input type="submit" name="COMMAND" value="‹ Prev">
<input type="submit" name="COMMAND" value="Save">
<input type="reset" name="NOTHING" value="Reset">
<input type="submit" name="COMMAND" value="Next ›">
<input type="button" name="NOTHING" value="Skip ›" onclick="location = 'yada-yada.asp';">
The row of buttons is a mix of submit, reset and JavaScript buttons. The order of buttons is subject to change, but in any case the save button remains between prev and next buttons.
The problem here is that when a user hits Enter to submit the form, the post variable "COMMAND" contains "Prev"; normal, as this is the first submit button on the form. I however want the "Next" button to be triggered when the user submits the form via the Enter button. It is kind of like setting it as the default submit button, even though there are other buttons before it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
第一个按钮始终是默认按钮;它无法改变。虽然您可以尝试使用 JavaScript 来修复它,但表单在没有脚本的浏览器中会出现意外行为,并且需要考虑一些可用性/可访问性的极端情况。例如,Zoran 链接到的代码将意外地在
中按 Enter 键时提交表单,这通常不会发生,并且不会捕获 IE在表单中的其他非字段内容上按 Enter 键提交表单的行为。因此,如果您使用该脚本单击表单中
中的某些文本并按 Enter 键,则会提交错误的按钮...特别危险,如果如该示例所示,真正的默认按钮是“删除”!
我的建议是忘记使用脚本黑客来重新分配默认值。按照浏览器的流程,将默认按钮放在前面。如果您无法修改布局来为您提供所需的屏幕顺序,那么您可以通过在源代码中首先添加一个虚拟的不可见按钮来实现,其名称/值与您想要默认的按钮相同:
(注意:定位用于将按钮推离屏幕,因为
display: none
和visibility: hide
对于按钮是否被视为默认按钮有浏览器变量的副作用以及是否已提交。)The first button is always the default; it can't be changed. Whilst you can try to fix it up with JavaScript, the form will behave unexpectedly in a browser without scripting, and there are some usability/accessibility corner cases to think about. For example, the code linked to by Zoran will accidentally submit the form on Enter press in a
<input type="button">
, which wouldn't normally happen, and won't catch IE's behaviour of submitting the form for Enter press on other non-field content in the form. So if you click on some text in a<p>
in the form with that script and press Enter, the wrong button will be submitted... especially dangerous if, as given in that example, the real default button is ‘Delete’!My advice would be to forget about using scripting hacks to reassign defaultness. Go with the flow of the browser and just put the default button first. If you can't hack the layout to give you the on-screen order you want, then you can do it by having a dummy invisible button first in the source, with the same name/value as the button you want to be default:
(note: positioning is used to push the button off-screen because
display: none
andvisibility: hidden
have browser-variable side-effects on whether the button is taken as default and whether it's submitted.)我的建议是不要对抗这种行为。您可以使用浮动有效地改变顺序。例如:
with:
将有效地反转顺序,因此“下一步”按钮将是按 Enter 键触发的值。
这种技术将涵盖许多情况,而无需诉诸更hacky JavaScript 方法。
My suggestion is don't fight this behaviour. You can effectively alter the order using floats. For example:
with:
will effectively reverse the order and thus the "Next" button will be the value triggered by hitting enter.
This kind of technique will cover many circumstances without having to resort to more hacky JavaScript methods.
将
type=submit
设置为您希望设为默认的按钮,并将type=button
设置为其他按钮。现在,在下面的表单中,您可以在任何输入字段中按 Enter 键,并且Render
按钮将起作用(尽管它是表单中的第二个按钮)。示例:
在 FF24 和 Chrome 35 中测试。
Set
type=submit
to the button you'd like to be default andtype=button
to other buttons. Now in the form below you can hit Enter in any input fields, and theRender
button will work (despite the fact it is the second button in the form).Example:
Tested in FF24 and Chrome 35.
Quick'n'dirty,您可以创建一个隐藏的提交按钮副本,按下 Enter 键时应该使用该按钮。
示例 CSS
示例 HTML
如果现在有人在您的表单中按 Enter 键,则(隐藏的)下一步按钮将用作提交者。
在 IE9、Firefox、Chrome 和 Opera 上测试
Quick'n'dirty you could create an hidden duplicate of the submit-button, which should be used, when pressing enter.
Example CSS
Example HTML
If someone now hits enter in your form, the (hidden) next-button will be used as submitter.
Tested on IE9, Firefox, Chrome and Opera
如果您使用的是 jQuery,则此解决方案来自评论 这里非常巧妙:
只需将
class="default"
添加到您想要设为默认值的按钮即可。它将该按钮的隐藏副本放在表单的开头。If you're using jQuery, this solution from a comment made here is pretty slick:
Just add
class="default"
to the button you want to be the default. It puts a hidden copy of that button right at the beginning of the form.我之所以复活这个是因为我正在研究一种非 JavaScript 的方法来做到这一点。我不喜欢关键处理程序,并且 CSS 定位内容导致 Tab 顺序中断,因为 CSS 重新定位不会改变 Tab 顺序。
我的解决方案基于 https://stackoverflow.com/a/9491141 的响应。
解决方案来源如下。 tabindex 用于纠正隐藏按钮的选项卡行为,以及 aria-hidden 以避免屏幕阅读器读出按钮/辅助设备识别按钮。
DOM 中的第一个按钮
DOM 中的第二个按钮
DOM Essential CSS 中的第三个按钮:
此解决方案的
I'm resurrecting this because I was researching a non-JavaScript way to do this. I wasn't into the key handlers, and the CSS positioning stuff was causing tab ordering to break since CSS repositioning doesn't change tab order.
My solution is based on the response at https://stackoverflow.com/a/9491141.
The solution source is below. tabindex is used to correct tab behaviour of the hidden button, as well as aria-hidden to avoid having the button read out by screen readers / identified by assistive devices.
1st button in DOM
2nd button in DOM
3rd button in DOM
Essential CSS for this solution:
另一种解决方案,使用 jQuery:
这应该适用于以下表单,使“更新”成为默认操作:
以及:
仅当表单上的输入字段具有焦点时,才会捕获 Enter 键。
Another solution, using jQuery:
This should work on the following forms, making "Update" the default action:
As well as:
This traps the Enter key only when an input field on the form has focus.
您不应该使用同名的按钮。这是糟糕的语义。相反,您应该修改后端以查找设置的不同名称值:
由于我不知道您在后端使用什么语言,所以我会给您一些伪代码:
You should not be using buttons of the same name. It's bad semantics. Instead, you should modify your backend to look for different name values being set:
Since I don't know what language you are using on the backend, I'll give you some pseudocode:
bobince 的解决方案有一个缺点,即创建一个可以用 Tab-d 覆盖的按钮,但否则无法使用。这可能会给键盘用户带来困惑。
另一种解决方案是使用鲜为人知的
form
属性:这是 标准 HTML,但遗憾的是 Internet Explorer 不支持。
bobince's solution has the downside of creating a button which can be Tab-d over, but otherwise unusable. This can create confusion for keyboard users.
A different solution is to use the little-known
form
attribute:This is standard HTML, however unfortunately not supported in Internet Explorer.