简体   繁体   English

.NET MVC - 在文本框中按 Enter 键不提交表单

[英].NET MVC - Pressing Enter key in textbox not submitting form

I have a very simple form like below.我有一个非常简单的表格,如下所示。

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

Pressing Enter key in the text field doesn't submit the form.在文本字段中按 Enter 键不会提交表单。 I'm wondering if there is a solution that doesn't involve any JavaScript.我想知道是否有不涉及任何 JavaScript 的解决方案。 Because when I searched online, there are people annoyed by this "default" behavior of the textbox and want to disable Enter key...因为当我在网上搜索时,有人对文本框的这种“默认”行为感到恼火,想要禁用 Enter 键...

Maybe it has something to do with the 'button' tag?也许它与“按钮”标签有关? I tried changing it to 'input' tag.我尝试将其更改为“输入”标签。 But pressing Enter still doesn't work.但是按 Enter 仍然不起作用。 The rendered HTML looks normal to me:呈现的 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>

The enter key will always submit the first HTML Form on the page. Enter 键将始终提交页面上的第一个 HTML 表单。 Make sure you don't have another form element hidden within your HTML.确保您的 HTML 中没有隐藏其他表单元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM