你如何禁用一个单一的点击在Angularjs prevent多次提交后提交按钮? [英] 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:
HTML
<button OnClientClick="disableButton()" type="submit">Submit</button>
JS
var disableButton = function (){ document.getElementById('<%= btnSubmit.ClientID %>').disabled = true; }
和这样的:
HTML
<button ng-disabled="isDisabled" type="submit">Submit</button>
JS
$scope.isDisabled = false;
var someFunc = function(){
$scope.isDisabled = true;}
无论是担任广告。任何其他建议?请只角的建议。谢谢你。
Neither worked as advertised. Any other suggestions? Please Angular suggestions only. Thank you.
推荐答案
您是非常接近的答案。你错过了唯一的办法就是使用调用按钮 someFunc()
函数 NG-点击
。
You were very close to the answer. The only thing you missed out was calling the someFunc()
function on button using ng-click
.
另一个问题是,在控制器的功能应该是 $ scope.someFunc()
,而不是 VAR someFunc()
The other issue is, in your controller the function should be $scope.someFunc()
and not var someFunc()
的例子:
您的index.html应该是这样的:
Working example: Your index.html should be like:
<html>
<head>
<script data-require="angular.js@1.3.15" 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>
和控制器的application.js如:
And your controller application.js be like:
angular.module('demo', [])
.controller('demoController',function($scope){
$scope.isDisabled = false;
$scope.disableButton = function() {
$scope.isDisabled = true;
}
});
这篇关于你如何禁用一个单一的点击在Angularjs prevent多次提交后提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!