假设您在HTML表单中创建了一个向导。一个按钮后退,一个按钮前进。因为当您按Enter键时,返回按钮首先出现在标记中,它将使用该按钮提交表单。

例子:

< >形式 <!—将光标移至该字段,按“Enter”。—> <input type="text" name="field1" /> <!这是提交的按钮——> <input type="submit" name="prev" value="Previous Page" /> <!——但这是我想提交的按钮——> <input type="submit" name="next" value=" next Page" /> > < /形式

我想要确定当用户按Enter键时使用哪个按钮提交表单。这样,当您按下Enter键时,向导将移动到下一页,而不是上一页。你必须使用tabindex来做这个吗?


当前回答

我认为这是一个简单的解决方法。将Previous按钮类型更改为button,并添加一个新的onclick属性,值为jQuery(this).attr('type','submit');

因此,当用户单击Previous按钮时,它的类型将被更改为submit,表单将与Previous按钮一起提交。

<form>
  <!-- Put your cursor in this field and press Enter -->
  <input type="text" name="field1" />

  <!-- This is the button that will submit -->
  <input type="button" onclick="jQuery(this).attr('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>

其他回答

要做到这一点,只需改变制表符顺序即可。保持简单。

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

如果你在一个页面上有多个活动按钮,你可以这样做:

将您想要在Enter键上触发的第一个按钮标记为表单上的默认按钮。对于第二个按钮,将其关联到键盘上的退格键。Backspace eventcode是8。

$(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"); } } }); <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script> <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>

这是我尝试过的:

你需要确保你给你的按钮不同的名字 编写if语句,在单击任意一个按钮时执行所需的操作。

 

<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中,

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

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

当我第一次遇到这种情况时,我想出了一个onclick()/JavaScript hack,当选择不是prev/next时,我仍然喜欢它的简单性。它是这样的:

@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.
    }
</script>

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

当单击任何一个提交按钮时,它将所需的操作存储在一个隐藏字段中(该字段是包含在与表单关联的模型中的字符串字段),并将表单提交给控制器,由控制器进行所有决定。在控制器中,你只需写:

// 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
}

您还可以使用数值操作代码来避免字符串解析,但是除非使用枚举,否则代码可读性较差、可修改性较差、自文档性较差,解析也很简单。

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

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

现在Next按钮将是默认的,另外你也可以添加默认属性,这样你的浏览器就会高亮显示它:

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