Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angularjs / Bootstrap why does my cancel button invoke the submit event

I have an Angular / Bootstrap form with a button type=submit and a plain button to cancel - and hide the form. When I click on the cancel, it calls the cancel event handler and then it calls the submit handler. ?? Here's the code as if have it in Plunker: http://plnkr.co/edit/Nsqy4DuVaoI04VObS2Zd?p=preview

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example33-production</title>


  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.14/angular.min.js"></script>



</head>
<body ng-app="submitExample">
   <script>
   angular.module('submitExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.list = [];
    $scope.text = 'hello';
    $scope.cancel = function(){
   alert('cancelling');
    }
  $scope.submitNewAccountForm = function(isValid){
  alert('in submit');
 }
  }]);
 </script>
<form ng-controller="ExampleController" name="newAccountForm" ng-submit="submitNewAccountForm(newAccountForm.$valid)" class="form-horizontal" novalidate>
  <fieldset>
    <!-- Form Name -->
    <legend>Account</legend>
    <!-- Text input-->
    <div class="control-group">
      <label class="control-label" for="name">Account name</label>
      <div class="controls">
        <input ng-model="name" id="name" name="name" type="text" placeholder="account" class="input-medium" required="">
        <p class="help-block">Required.</p>
      </div>
    </div>
    <!-- Select Basic -->
    <div class="control-group">
      <label class="control-label" for="type">Account type</label>
      <div class="controls">
        <select ng-model="type" id="type" name="type" class="input-medium">
          <option>client</option>
          <option>provider</option>
        </select>
      </div>
    </div>
    <!-- Button (Double) -->
    <div class="control-group">
      <label class="control-label" for="cancelaccountadd"></label>
      <div class="controls">
        <button type="submit" id="doaccountadd" name="doaccountadd" class="btn btn-success">Save</button>
        <button ng-click="cancel()" id="cancelaccountadd" name="cancelaccountadd" class="btn btn-danger">Cancel</button>
      </div>
    </div>

  </fieldset>
</form> 
</body>
</html>
like image 903
JohnL Avatar asked Aug 12 '26 02:08

JohnL


1 Answers

On button elements, submit is the default type attribute. change that to type="button" and it will no longer call the submit event.

Plunker

like image 66
Morgan Delaney Avatar answered Aug 14 '26 15:08

Morgan Delaney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!