Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 在cshtml中按enter键提交_Jquery_Angularjs_Asp.net Mvc 4 - Fatal编程技术网

Jquery 在cshtml中按enter键提交

Jquery 在cshtml中按enter键提交,jquery,angularjs,asp.net-mvc-4,Jquery,Angularjs,Asp.net Mvc 4,对MVC来说非常陌生,正在尝试找出如何将处理程序附加到submit按钮,以便用户不必单击submit按钮 我有以下JavaScript: //fix to add submit to the enter button function keypressHandler(e) { if (e.which == 13) { e.preventDefault(); //stops default action

对MVC来说非常陌生,正在尝试找出如何将处理程序附加到submit按钮,以便用户不必单击submit按钮

我有以下JavaScript:

            //fix to add submit to the enter button
        function keypressHandler(e) {
            if (e.which == 13) {
                e.preventDefault(); //stops default action: submitting form
                $(this).blur();
                $('#btnLoginSubmit').focus().click();//give your submit an ID
            }
        }

        $('#myForm').keypress(keypressHandler);
以下是_Layout.cshtml页面的标记:

<div ng-controller="LoginController">
<script type="text/ng-template" id="loginForm.html">
            <div class="modal-header">
                <h3>Please log in</h3>
            </div>
            <form ng-submit="submit()">
                <div class="modal-body">
                    <label>User name</label>
                    <input type="text" ng-model="user.userId" />
                    <label>Password</label>
                    <input type="password" ng-model="user.password" />
                </div>
                <div class="modal-footer">
                    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
                    <input type="submit" class="btn primary-btn" value="Submit" />
                </div>
            </form>
        </script>

请登录
用户名
密码
取消


我很难弄清楚如何将脚本附加到实际的提交按钮。我向添加了一个id,但是JS正在查找表单名称(我知道它不是myForm),它似乎没有在任何地方定义。感觉这应该很简单,但我遗漏了一些东西。

您需要为
表单提供
id

<form id="myform" ng-submit="submit()">
最后,您需要将其连接到文档:

$(document).keypress(keypressHandler);

取而代之的是,我不得不说,我有点困惑,为什么在表单控件中输入键不起作用-这是您实际经历的吗?我不完全确定发生了什么,但它似乎是在点击取消按钮而不是提交按钮。所以你输入你的凭证,点击回车键,它就会关闭弹出窗口。然后我必须刷新页面。不确定“取消”按钮是否以某种方式获得了焦点。因此我切换了按钮,将“提交”按钮放在第一位,现在它工作了。有点尴尬,我没有先想到这一点。新的演出,很多新技术。谢谢你的帮助。
$(document).keypress(keypressHandler);