2016-02-04 223 views
0

我创建了一个使用Angular和Ionic的混合应用程序,但是当我填写我的表单并按下GO按钮或Android上的箭头按钮时,它会提交我的表单,即使它不是有效的然而。我试图检测到按键和return:false;,但它仍然提交。'GO'按钮提交表单

<form novalidate name="questionForm" id="questionForm" ng-submit="submit()"> 
     <input name="question" ng-model="form.question" ng-minlength="10" required autofocus/> 
     <div id="answers"> 
<input ng-model="choice.choice" placeholder="Voeg antwoord toe" required ng-keypress="disableGo()" /> 
     </div> 
     <button type="submit" ng-disabled="questionForm.$invalid" class="circle-btn btn-send ion-ios-paperplane"></button> 
    </form> 

按键时功能:

$scope.disableGo = function() { 
     var code = (event.keyCode ? event.keyCode : event.which); 
     if ((code == 13) || (code == 10)) { 
      return false; 
     } 
    }; 

回答

1

使用type='button'而不是submit

你不需要disableGo

试试这个:

<form novalidate name="questionForm" id="questionForm"> 
 
    <input name="question" ng-model="form.question" ng-minlength="10" required autofocus/> 
 
    <div id="answers"> 
 
    <input ng-model="choice.choice" placeholder="Voeg antwoord toe" required ng-keypress="disableGo()" /> 
 
    </div> 
 
    <button type="button" ng-disabled="questionForm.$invalid" class="circle-btn btn-send ion-ios-paperplane" ng-click="submit()"></button> 
 
</form>

使用ng-clickbutton,而不是ng-submit

+0

哦,没错,那是有效的。谢谢。我会在5分钟内接受你的回答 – Greg

1

添加类型到你的按钮按钮可以帮助你

<button type="button"></button> 
+0

然后我的表单就不会被提交时,我按下提交按钮。 Angular使用'ng-submit'so它寻找一个'type = submit'button – Greg

1

您可以检查此

在控制器中添加功能:

$scope.canSubmit = function() { 
     return ($scope.questionForm.$dirty && $scope.questionForm.$invalid) || $scope.questionForm.$pristine 
}; 

在HTML:使用type="button"

<button type="button" ng-disabled="canSubmit()" class="circle-btn btn-send ion-ios-paperplane"></button>