如何按回车键触发我的mvc表单登录?

时间:2018-01-19 11:35:53

标签: asp.net asp.net-mvc-4

我的login.cshtml中有一个小的登录表单,现在提交按钮工作正常,但是我还需要按Enter键进行表单工作,我应该使用一些jquery代码还是可以在没有jquery的情况下解决这个问题?

  @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form", id = "login_form" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                <div class="form-group">
                    @Html.LabelFor(m => m.Email)
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    @Html.LabelFor(m => m.Password)
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                </div>

                <div class="form-group">
                    <div class="margin-bottom-20">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe, new { @class = "pull-left" })
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <input type="submit" class="hidden" />
                    <button type="button" id="btn-login" data-style="expand-right" class="btn btn-action btn-block ladda-button"><span class="ladda-label">Login</span></button>
                </div>

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题

$(document).ready(function () {
    $("#Formbegin").keypress(function (e) {
        keycode = e.keyCode || e.charCode || e.which //for cross browser
        if (keycode == 13)    //keyCode for the Enter / Return key
        {
            var defaultbutton = $(this).attr("DefaultButton");
            $("#" + defaultbutton).click();
            return false;
        }
    });
});