如果一个 HTML 表单有两个 <input type="submit"> 按钮,我怎么知道哪个被点击了?

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

If an HTML form has two <input type="submit"> buttons, how do I know which got clicked?

htmlformssubmit

提问by bodacydo

Suppose I have the following HTML form:

假设我有以下 HTML 表单:

<form>
...
<input type="submit" name="queue" value="Queue item">
<input type="submit" name="submit" value="Submit item">
</form>

How do I know which button the user clicked (without using javascript)?

我怎么知道用户点击了哪个按钮(不使用 javascript)?

I looked at submitted data and it seems that when "Queue Item"is clicked then "queue" = "Queue Item"gets sent to the server. And when "Submit item"is clicked then "submit" = "Submit item"sets sent.

我查看了提交的数据,似乎"Queue Item"点击后"queue" = "Queue Item"会被发送到服务器。当"Submit item"点击然后"submit" = "Submit item"设置发送。

Can I rely on this behavior? Is it documented somewhere in the standard on HTML forms? How do you guys do it?

我可以依赖这种行为吗?它是否记录在 HTML 表单的标准中?你们是怎么做的?

回答by Tim Perry

Yes, you can rely on this; it's fully documented here. The specific relevant lines say:

是的,您可以信赖它;这里有完整的文档。具体的相关行说:

When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form. Those controls for which name/value pairs are submitted are called successful controls.

当提交表单进行处理时,某些控件的名称与其当前值成对,并且这些对与表单一起提交。提交名称/值对的那些控件称为成功控件。

and

If a form contains more than one submit button, only the activated submit button is successful.

如果一个表单包含多个提交按钮,则只有激活的提交按钮才能成功。

回答by mauris

Yep you can rely on that behaviour.

是的,您可以依靠这种行为。

When <input type="submit" name="queue" value="Queue item">is clicked, the field "queue" will be set and "submit" will not be.

<input type="submit" name="queue" value="Queue item">被点击时,现场“排队”将被设置和“提交”不会。

Whereas when the other gets clicked, the field "submit" will be set, and "queue" will not be.

而当另一个被点击时,将设置字段“提交”,而不会设置“队列”。

If you're not assured by this, you can split them into 2 forms and work on it that way.

如果您对此不放心,您可以将它们分成 2 种形式并以这种方式进行处理。

回答by Pentium10

You can rely on this behavior. You get the value of the input. I would use javascriptto toggle a hidden form value, but since you mentioned no javascriptyou do not have multiple choices.

您可以信赖这种行为。你得到输入的值。我会javascript用来切换隐藏的表单值,但既然你提到了不,javascript你就没有多个选择。

It's a standard. Since it's an inputtag, and has a value, that means you get the valuesubmitted.

这是一个标准。因为它是一个input标签,并且有一个value,这意味着你得到了value提交。

回答by MidnightLightning

Split the form into two forms, replicating any other inputs needed by the other action. Or, if you really just need to know if the user wants to "queue vs. submit" the item, change both submit buttons to radio selections to toggle between the two options, and have a new, separate "submit the form" button.

将表单拆分为两个表单,复制其他操作所需的任何其他输入。或者,如果您真的只需要知道用户是否想要“排队还是提交”该项目,请将两个提交按钮更改为单选选项以在两个选项之间切换,并有一个新的、单独的“提交表单”按钮。

In that situation if you want a one-click option, you could use Javascript to detect when one of the radio buttons is selected, and auto-submit the form instantly. (Using Javascript for user interface, rather than form handling)

在这种情况下,如果您想要一键式选项,您可以使用 Javascript 来检测何时选择了一个单选按钮,并立即自动提交表单。(使用 Javascript 作为用户界面,而不是表单处理)