Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable the submit button after a single click to prevent multiple submissions in Angularjs?

I've looked all over the web including several stack overflow examples for a solution to the question. To be specific, I tried this:

var disableButton = function() {    document.getElementById('<%= btnSubmit.ClientID %>').disabled = true;  }    $scope.isDisabled = false;  var someFunc = function() {    $scope.isDisabled = true;  }
<button OnClientClick="disableButton()" type="submit">Submit</button>  <button ng-disabled="isDisabled" type="submit">Submit</button>

Neither worked as advertised. Any other suggestions? Please Angular suggestions only. Thank you.

like image 502
rashadb Avatar asked May 01 '15 09:05

rashadb


People also ask

How do you disable the submit button after clicking it?

1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.

How do I enable a submit button?

Approach: To enable or disable the form submit button, we use the below code snippet. $('#enabled'). click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button').


1 Answers

You were very close to the answer. The only thing you missed out was calling the someFunc() function on button using ng-click.

The other issue is, in your controller the function should be $scope.someFunc() and not var someFunc()

Working example: Your index.html should be like:

<html>    <head>     <script data-require="[email protected]" data-semver="1.3.15" src="https://code.angularjs.org/1.3.15/angular.js"></script>     <link rel="stylesheet" href="style.css" />     <script src="script.js"></script>     <script src="application.js"></script>   </head>    <body ng-app="demo" ng-controller="demoController">           <button type="submit" ng-disabled="isDisabled" ng-click="disableButton()"> Click me to disable myself</button>   </body>  </html> 

And your controller application.js be like:

angular.module('demo', [])     .controller('demoController',function($scope){      $scope.isDisabled = false;      $scope.disableButton = function() {         $scope.isDisabled = true;     }      }); 
like image 154
Wahid Kadwaikar Avatar answered Sep 30 '22 01:09

Wahid Kadwaikar