简体   繁体   English

使用angular.js使用Enter键提交表单

[英]submit form using Enter key using angular.js

I need one help.I need to submit the form when user will press the enter key using Angular.js. 我需要一个帮助。当用户使用Angular.js按下Enter键时,我需要提交表单。 I am explaining my code below. 我在下面解释我的代码。

    <form name="billdata" id="billdata" enctype="multipart/form-data" novalidate>
    <div id="SHOWDATA">
        <div id="transactionsPortlet" class="panel-collapse collapse in">
            <div class="portlet-body">
                <div class="totalaligndiv">
                    <div class="col-md-6">
                        <div class="input-group bmargindiv1 col-md-12">
                            <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Subject Type:</span>
                            <input type="text" name="shortname" id="resourcesub" class="form-control" placeholder="Add Your Subject Type " ng-model="subject" ng-keypress="clearField('resourcesub');">
                        </div>
                    </div>

                    <div class="col-md-6">
                        <div class="input-group bmargindiv1 col-md-12">
                            <span class="input-group-addon ndrftextwidth text-right" style="width:180px">No of Classes:</span>
                            <select class="form-control" id="resourceperiod" ng-model="period" ng-change="removeBorder('resourceperiod',subject);">
                                <option value="">Select Class</option>
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                            </select>
                        </div>
                    </div>

                    <div class="clearfix"></div>
                    <div style="text-align:center; padding-top:10px;">
                        <input type="button" class="btn btn-success" ng-click="addClassData();" id="addProfileData" ng-value="buttonName" />
                        <input type="button" class="btn btn-red" ng-click="cancelClassData();" id="cancelProfileData" ng-value="cancelbuttonName" ng-show="showCancel" />

                    </div>

                    <div class="clearfix"></div>

                </div>
            </div>
        </div>
    </div>
</form>

Here i need if user is pressing enter key with out providing any data,the validating will display.If user is pressing enter key by providing all data the form will submit.Now i can do the operations by clicking the button but here i need both.Please help me. 如果用户在不输入任何数据的情况下按Enter键,则将显示验证。如果用户通过提供将提交的所有数据数据按Enter键,现在我可以通过单击按钮进行操作,但在此我需要。请帮我。

You can put ng-submit like : 您可以像这样输入ng-submit:

<form name="billdata" id="billdata" enctype="multipart/form-data" novalidate ng-submit="addClassData();">

or if you want to keep it button instead of submit buttton than below directive may help you. 或者,如果您想保持按钮状态而不是提交按钮,则以下指令可能会对您有所帮助。

angular.module('NgEnter', [])
    .directive('ngEnter', function() {
        return function(scope, element, attrs) {
            element.bind("keypress", function(e) {
                if(e.which === 13) {
                    scope.$apply(function(){
                        scope.$eval(attrs.ngEnter, {'e': e});
                    });
                    e.preventDefault();
                }
            });
        };
    });

and than call this directive to 而不是将此指令称为

<form name="billdata" id="billdata" enctype="multipart/form-data" novalidate ng-enter="addClassData();">

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

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