HTML表单中的多个提交按钮

HTML表单中的多个提交按钮

Multiple submit buttons in an HTML form

假设您以HTML表单创建向导。 一键向后退,一键向前。 由于当您按Enter时,后退按钮首先出现在标记中,因此它将使用该按钮来提交表单。

例:

1
2
3
4
5
6
7
8
9
10
<form>
  <!-- Put your cursor in this field and press Enter -->
  <input type="text" name="field1" />

  <!-- This is the button that will submit -->
  <input type="submit" name="prev" value="Previous Page" />

  <!-- But this is the button that I WANT to submit -->
  <input type="submit" name="next" value="Next Page" />
</form>

我想确定当用户按下Enter时使用哪个按钮提交表单。 这样,当您按Enter时,向导将移至下一页,而不是上一页。 您必须使用tabindex来执行此操作吗?


我希望这有帮助。我只是在做float右边按钮的把戏。

这样,Prev按钮位于Next按钮的左侧,但是Next在HTML结构中排在首位:

1
2
3
4
5
6
.f {
  float: right;
}
.clr {
  clear: both;
}
1
2
3
4
5
6
7
8
<form action="action" method="get">
  <input type="text" name="abc">
 
    <input type="submit" class="f" name="next" value="Next">
    <input type="submit" class="f" name="prev" value="Prev">
    <!-- This div prevents later elements from floating with the buttons. Keeps them 'inside' div#buttons -->
 
</form>

与其他建议相比的好处:没有JavaScript代码,无法访问,并且两个按钮都保持type="submit"


将以前的按钮类型更改为这样的按钮:

1
<input type="button" name="prev" value="Previous Page" />

现在,"下一步"按钮将成为默认按钮,此外,您还可以向其中添加default属性,以便浏览器将其突出显示,如下所示:

1
<input type="submit" name="next" value="Next Page" default />

为您的提交按钮命名,如下所示:

1
2
<input type="submit" name="submitButton" value="Previous Page" />
<input type="submit" name="submitButton" value="Next Page" />

当用户按下Enter并将请求发送到服务器时,您可以在服务器端代码中检查submitButton的值,该代码包含形式为name/value对的集合。例如,在ASP Classic中:

1
2
3
4
5
If Request.Form("submitButton") ="Previous Page" Then
    ' Code for the previous page
ElseIf Request.Form("submitButton") ="Next Page" Then
    ' Code for the next page
End If

参考:在单个表单上使用多个提交按钮


如果默认情况下使用第一个按钮的事实在浏览器之间是一致的,则将它们正确放置在源代码中,然后使用CSS切换它们的明显位置。

例如,按float左右键可以在视觉上切换它们。


有时@palotasb提供的解决方案是不够的。在某些情况下,例如在"下一个和上一个"之类的按钮上方放置一个"过滤器"提交按钮。我发现了一种解决方法:将需要充当默认提交按钮的提交按钮复制到一个隐藏的div中,然后将其放在表单中任何其他提交按钮之上。
从技术上讲,按Enter键然后单击可见的Next按钮时,将由其他按钮提交。但是由于名称和值相同,所以结果没有差异。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>
<head>
    <style>
        div.defaultsubmitbutton {
            display: none;
        }
    </style>
</head>
<body>
    <form action="action" method="get">
       
            <input type="submit" name="next" value="Next">
       
        <p>
<input type="text" name="filter"><input type="submit" value="Filter">
</p>
        <p>
Filtered results
</p>
        <input type="radio" name="choice" value="1">Filtered result 1
        <input type="radio" name="choice" value="2">Filtered result 2
        <input type="radio" name="choice" value="3">Filtered result 3
                       
            <input type="submit" name="prev" value="Prev">
            <input type="submit" name="next" value="Next">
       
    </form>
</body>
</html>

如果您真的只希望它像安装对话框一样工作,则只需将焦点放在"下一步"按钮OnLoad上即可。

这样,如果用户点击Return,则表单将提交并继续前进。如果要返回,可以单击Tab或单击按钮。


纯HTML不能做到这一点。您必须依靠JavaScript才能实现此技巧。

但是,如果在HTML页面上放置两个表单,则可以执行此操作。

Form1将具有上一个按钮。

Form2将具有任何用户输入+下一个按钮。

当用户在Form2中按下Enter时,将触发"下一步提交"按钮。


您可以使用CSS来实现。

首先使用Next按钮将按钮放入标记中,然后使用Prev按钮进行标记。

然后使用CSS定位它们,使其以您想要的方式显示。


我将使用JavaScript提交表单。该函数将由form元素的OnKeyPress事件触发,并将检测是否选择了Enter键。如果是这种情况,它将提交表格。

以下是两个页面,提供了有关操作方法的技术:1、2。基于这些,这里是用法示例(基于此处):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<SCRIPT TYPE="text/javascript">//<!--
function submitenter(myfield,e) {
  var keycode;
  if (window.event) {
    keycode = window.event.keyCode;
  } else if (e) {
    keycode = e.which;
  } else {
    return true;
  }

  if (keycode == 13) {
    myfield.form.submit();
    return false;
  } else {
    return true;
  }
}
//-->

<INPUT NAME="MyText" TYPE="Text" onKeyPress="return submitenter(this,event)" />


在大多数浏览器中,此方法无需JavaScript或CSS即可运行:

1
2
3
4
5
6
7
8
9
10
<form>
    <p>
<input type="text" name="field1" />
</p>
    <p>

    <button type="button">Previous Page</button>
    <button type="submit">Next Page</button>
</p>
</form>

Firefox,Opera,Safari和Google Chrome均可使用。
和往常一样,InternetExplorer就是问题所在。

启用JavaScript时,此版本适用:

1
2
3
4
5
6
7
8
9
10
<form>
    <p>
<input type="text" name="field1" />
</p>
    <p>

    <button type="button" onclick="window.location='previous.html'">Previous Page</button>
    <button type="submit">Next Page</button>
</p>
</form>

因此,此解决方案的缺陷是:

如果您在关闭JavaScript的情况下使用InternetExplorer,则上一页不起作用。

请注意,后退按钮仍然有效!


如果一页上有多个活动按钮,则可以执行以下操作:

Enter按键上将要触发的第一个按钮标记为表单上的默认按钮。对于第二个按钮,将其与键盘上的Backspace按钮关联。 Backspace事件代码为8。

1
2
3
4
5
6
7
8
9
$(document).on("keydown", function(event) {
  if (event.which.toString() =="8") {
    var findActiveElementsClosestForm = $(document.activeElement).closest("form");

    if (findActiveElementsClosestForm && findActiveElementsClosestForm.length) {
      $("form#" + findActiveElementsClosestForm[0].id +" .secondary_button").trigger("click");
    }
  }
});
1
2
3
4
5
6
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js">

<form action="action" method="get" defaultbutton="TriggerOnEnter">
  <input type="submit" id="PreviousButton" name="prev" value="Prev" class="secondary_button" />
  <input type="submit" id='TriggerOnEnter' name="next" value="Next" class="primary_button" />
</form>


更改制表符顺序应该是完成此操作所需要的全部工作。把事情简单化。

另一个简单的选择是将后退按钮放在HTML代码中的"提交"按钮之后,但将其浮动到左侧,以便它出现在页面上的"提交"按钮之前。


另一个简单的选择是将后退按钮放在HTML代码中的"提交"按钮之后,但将其浮动到左侧,以便它在页面上出现在"提交"按钮之前。

更改制表符顺序应该是完成此操作所需要的全部工作。把事情简单化。


第一次遇到这种情况时,我想到了onclick()/ JavaScript hack,因为选择不是上一个/下一个,为简单起见我仍然喜欢。它是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@model myApp.Models.myModel

<script type="text/javascript">
    function doOperation(op) {
        document.getElementById("OperationId").innerText = op;
        // you could also use Ajax to reference the element.
    }


<form>
  <input type="text" id ="TextFieldId" name="TextField" value="" />
  <input type="hidden" id="OperationId" name="Operation" value="" />
  <input type="submit" name="write" value="Write" onclick='doOperation("Write")'/>
  <input type="submit" name="read" value="Read" onclick='doOperation("Read")'/>
</form>

单击任一"提交"按钮后,它将所需的操作存储在隐藏字段(这是与表单关联的模型中包含的字符串字段)中,并将表单提交给控制器,由控制器进行所有决策。在Controller中,您只需编写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Do operation according to which submit button was clicked
// based on the contents of the hidden Operation field.
if (myModel.Operation =="Read")
{
     // Do read logic
}
else if (myModel.Operation =="Write")
{
     // Do write logic
}
else
{
     // Do error logic
}

您也可以使用数字操作代码略微加强此功能,以避免字符串解析,但是除非您使用枚举,否则代码的可读性,可修改性和自文档性都较低,并且无论如何解析都是微不足道的。


1
2
<input type="submit" name="perv" value="Previous Page">
<input type="submit" name="prev" value="Next Page">

保持所有提交按钮的名称相同-" prev"。

唯一的区别是具有唯一值的value属性。创建脚本时,这些唯一值将有助于我们确定按下了哪个提交按钮。

并编写以下代码:

1
2
3
4
5
6
    btnID =""
if Request.Form("prev") ="Previous Page" then
    btnID ="1"
else if Request.Form("prev") ="Next Page" then
    btnID ="2"
end if

这是我尝试过的:

  • 您需要确保为按钮指定不同的名称
  • 编写一个if语句,如果单击任一按钮,它将执行所需的操作。
  • 1
    2
    3
    4
    5
    6
    <form>
        <input type="text" name="field1" /> <!-- Put your cursor in this field and press Enter -->

        <input type="submit" name="prev" value="Previous Page" /> <!-- This is the button that will submit -->
        <input type="submit" name="next" value="Next Page" /> <!-- But this is the button that I WANT to submit -->
    </form>

    在PHP中,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    if(isset($_POST['prev']))
    {
        header("Location: previous.html");
        die();
    }

    if(isset($_POST['next']))
    {
        header("Location: next.html");
        die();
    }

    来自https://html.spec.whatwg.org/multipage/forms.html#implicit-submission

    A form element's default button is the first submit button in tree
    order whose form owner is that form element.

    If the user agent supports letting the user submit a form implicitly
    (for example, on some platforms hitting the"enter" key while a text
    field is focused implicitly submits the form)...

    将下一个输入设为type =" submit"并将先前的输入更改为type =" button"应该会提供所需的默认行为。

    1
    2
    3
    4
    5
    6
    <form>
       <input type="text" name="field1" /> <!-- put your cursor in this field and press Enter -->

       <input type="button" name="prev" value="Previous Page" /> <!-- This is the button that will submit -->
       <input type="submit" name="next" value="Next Page" /> <!-- But this is the button that I WANT to submit -->
    </form>

    当我发现ASP按钮具有一个名为UseSubmitBehavior的属性,该属性允许您设置要提交的内容时,仅在使用ASP.NET控件时,我遇到了这个问题。

    1
     

    以防万一有人在寻找使用ASP.NET按钮的方法。


    使用JavaScript(此处为jQuery),可以在提交表单之前禁用prev按钮。

    1
    2
    3
    4
    5
    $('form').on('keypress', function(event) {
        if (event.which == 13) {
            $('input[name="prev"]').prop('type', 'button');
        }
    });


    您可以使用Tabindex解决此问题。 同样,更改按钮的顺序将是更有效的方法。

    更改按钮的顺序并添加float值,以将它们分配给想要在HTML视图中显示的所需位置。


    尝试这个..!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    <form>
      <input type="text" name="Name" />
      <!-- Enter the value -->

      <input type="button" name="prev" value="Previous Page" />
      <!-- This is the button that will submit -->
      <input type="submit" name="next" value="Next Page" />
      <!-- But this is the button that I WANT to submit -->
    </form>


    我以这种方式解决了一个非常类似的问题:

  • 如果启用了JavaScript(在当今大多数情况下),则所有提交按钮都将通过JavaScript(jQuery)在页面加载时"降级"为按钮。也可以通过JavaScript处理"降级"按钮上的单击事件。

  • 如果未启用JavaScript,则使用多个提交按钮将表单提供给浏览器。在这种情况下,在表单中的textfield上单击Enter将使用第一个按钮(而不是预期的默认值)提交表单,但是至少该表单仍然可用:您可以同时使用prev和next按钮提交。

  • 工作示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    <html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
        </head>

        <body>
            <form action="http://httpbin.org/post" method="post">
                If JavaScript  is disabled, then you CAN submit the form
                with button1, button2 or button3.

                If you press enter on a text field, then the form is
                submitted with the first submit button.

                If JavaScript is enabled, then the submit typed buttons
                without the 'defaultSubmitButton' style are converted
                to button typed buttons.

                If you press Enter on a text field, then the form is
                submitted with the only submit button
                (the one with class defaultSubmitButton)

                If you click on any other button in the form, then the
                form is submitted with that button's value.

                <br />

                <input type="text" name="text1"></input>
                <button type="submit" name="action" value="button1">button 1</button>
                <br />

                <input type="text" name="text2"></input>
                <button type="submit" name="action" value="button2">button 2</button>
                <br />

                <input type="text" name="text3"></input>
                <button class="defaultSubmitButton" type="submit" name="action" value="button3">default button</button>
            </form>

           
                $(document).ready(function(){

                    /* Change submit typed buttons without the 'defaultSubmitButton'
                       style to button typed buttons */
                    $('form button[type=submit]').not('.defaultSubmitButton').each(function(){
                        $(this).attr('type', 'button');
                    });

                    /* Clicking on button typed buttons results in:
                       1. Setting the form's submit button's value to
                          the clicked button's value,
                       2. Clicking on the form's submit button */
                    $('form button[type=button]').click(function( event ){
                        var form = event.target.closest('form');
                        var submit = $("button[type='submit']",form).first();
                        submit.val(event.target.value);
                        submit.click();
                    });
                });
           
        </body>
    </html>


    使用您给出的示例:

    1
    2
    3
    4
    5
    <form>
        <input type="text" name="field1" /><!-- Put your cursor in this field and press Enter -->
        <input type="submit" name="prev" value="Previous Page" /> <!-- This is the button that will submit -->
        <input type="submit" name="next" value="Next Page" /> <!-- But this is the button that I WANT to submit -->
    </form>

    如果单击"上一页",则只会提交"上一页"的值。如果单击"下一页",则仅会提交"下一个"的值。

    但是,如果您在表单上的某处按Enter,则" prev"和" next"都不会提交。

    因此,使用伪代码可以执行以下操作:

    1
    2
    3
    4
    5
    6
    If"prev" submitted then
        Previous Page was click
    Else If"next" submitted then
        Next Page was click
    Else
        No button was click


    推荐阅读