检查提交按钮ID并执行不同的功能

时间:2018-03-10 18:26:50

标签: angularjs

在我的表单中,我使用两个提交按钮。当我提交按钮时,首先需要检查按钮ID并相应地执行功能。请检查我的代码。

$scope.saveme = function(user,data) {

alert(id);
}
<form name="myform" ng-submit="saveme(user,data)">
Name : <input type="text" ng-model="user.name"/>
Age : <input type="text" ng-model="user.age"/>

<button type="submit" id="s" data="{{button.id}}"> Save</button>
<button type="submit" id="ss" data="{{button.id}}">Save with Exit </button>
</form>

实际上我需要提醒我点击的按钮ID。

3 个答案:

答案 0 :(得分:1)

您可以这样做:

$scope.saveme = function(user,exit) {
  if(exit){
     //do something
  }else{
     //do something else
 } 
 alert(id);

}

<form name="myform">
     Name : <input type="text" ng-model="user.name"/>
     Age : <input type="text" ng-model="user.age"/>

     <button type="submit" id="s" ng-click="saveme(user,false)"> Save</button>
     <button type="submit" id="ss" ng-click="sameme(user,true)">Save with Exit</button>
 </form>

您将知道单击了哪个按钮检查退出参数值

答案 1 :(得分:0)

您可以尝试以下代码。

如果您的第一个按钮返回true,则第二个按钮将不会触发,如果第二个按钮返回false,则第二个按钮将触发。

&#13;
&#13;
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

 <form name="myform">
Name : <input type="text" ng-model="user.name"/>
Age : <input type="text" ng-model="user.age"/>

<button type="submit" id="s" ng-click="saveme(1) || saveme1()" > Save</button>
</form>

</div>
</body>
</html>



<script>
var myApp = angular.module("myApp",[]);

myApp.controller('myCtrl', function($scope) {
   $scope.saveme = function(user) {
   if(user === 1){  
   alert('first button');
   return false
   } else {
   alert('first button fails');
    return true;

}
};
$scope.saveme1 = function(user,data) {

alert('second button');
};
});

</script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

<form name="myform" ng-submit="saveme(user,data)">
Name : <input type="text" ng-model="user.name"/>
Age : <input type="text" ng-model="user.age"/>

<button type="submit" id="s" ng-click="save('s')"> Save</button>
<button type="submit" id="ss" ng-click=saveExit"('ss')" >Save with Exit </button>
</form>

$scope.saveExit = function(val)
{
alert(val);
}
$scope.save= function(val)
{
alert(val);
}