首页 > 解决方案 > .NET MVC - 在文本框中按 Enter 键未提交表单

问题描述

我有一个非常简单的表格,如下所示。

@using (Html.BeginForm())
{
    ...
    @Html.TextBoxFor(x => x.Input);
    ...
    <button type="submit">Submit</button>
}

在文本字段中按 Enter 键不会提交表单。我想知道是否有不涉及任何 JavaScript 的解决方案。因为当我在网上搜索时,有人对文本框的这种“默认”行为感到恼火,想要禁用 Enter 键...

也许它与“按钮”标签有关?我尝试将其更改为“输入”标签。但是按 Enter 仍然不起作用。呈现的 HTML 对我来说看起来很正常:

<form action="/[ControllerName]/[ActionName]/" method="post" novalidate="novalidate">
    ...
    <input id="Input1" name="Input" type="text" value="" aria-required="true">
    ...
    <button type="submit">Submit</button>
    ...
</form>

标签: c#htmlasp.net-mvcformssubmit

解决方案


Enter 键将始终提交页面上的第一个 HTML 表单。确保您的 HTML 中没有隐藏其他表单元素。


推荐阅读