Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button press fired on angular form submit

Tags:

angularjs

I have a form with an input text field as well as a button. I'd like to capture the user pressing enter in the text field - not to submit the form, but to trigger another action. The button has its own separate click handler that for some reason gets fired when I press enter in the text field.

example: http://jsfiddle.net/T8zLq/1/

<form onsubmit="return false" ng-submit="mysubmit()" ng-controller="TestCtrl" ng-app="Test">
    <input type="text" />
    <button ng-click="test()">X</button>
</form>

var app=angular.module("Test", []);

app.controller("TestCtrl", ["$scope", function($scope) {
    $scope.test = function() {alert('lol');  };
    $scope.mysubmit = function() {alert('submit');};
}]);

Why does this happen?

like image 584
user3088880 Avatar asked Dec 12 '22 09:12

user3088880


1 Answers

set type='button' to your button

<form ng-submit="mysubmit()" ng-controller="TestCtrl">
    <input type="text" />
    <button ng-click="test()" type='button'>X</button>
</form>

Demo

More

like image 90
Jonathan de M. Avatar answered Jan 04 '23 17:01

Jonathan de M.