Very new to MVC and trying to figure out how to attach a handler to a submit button so the user does not have to click the submit button.
对MVC来说很新,并试图弄清楚如何将处理程序附加到提交按钮,这样用户就不必单击提交按钮。
I have the following JavaScript:
我有以下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);
and here is the markup for the _Layout.cshtml page:
这是_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>
I'm having a hard time figuring out how to attach the script to the actual submit button. I added an id to but the JS is looking for the form name (I know it's not myForm), which doesn't appear to be defined anywhere. Feel like this should be easy but I'm missing something.
我很难弄清楚如何将脚本附加到实际的提交按钮。我添加了一个id,但JS正在寻找表单名称(我知道它不是myForm),它似乎没有在任何地方定义。感觉这应该很容易,但我错过了一些东西。
1 个解决方案
#1
2
You need to give the form
an id
:
你需要给表单一个id:
<form id="myform" ng-submit="submit()">
but furthermore, you only need to submit the form itself:
但此外,您只需提交表单本身:
function keypressHandler(e) {
if (e.which == 13) {
e.preventDefault(); //stops default action
$('#myform').submit();
}
}
and then finally, you'd really need to hook it up to the document:
最后,你真的需要把它连接到文档:
$(document).keypress(keypressHandler);
#1
2
You need to give the form
an id
:
你需要给表单一个id:
<form id="myform" ng-submit="submit()">
but furthermore, you only need to submit the form itself:
但此外,您只需提交表单本身:
function keypressHandler(e) {
if (e.which == 13) {
e.preventDefault(); //stops default action
$('#myform').submit();
}
}
and then finally, you'd really need to hook it up to the document:
最后,你真的需要把它连接到文档:
$(document).keypress(keypressHandler);