如何在单击后禁用提交按钮以防止在 Angularjs 中多次提交? [英] How do you disable the submit button after a single click to prevent multiple submissions in Angularjs?

查看:28
本文介绍了如何在单击后禁用提交按钮以防止在 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>

都没有像宣传的那样工作.还有其他建议吗?请仅提供 Angular 建议.谢谢.

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

推荐答案

您已经非常接近答案了.您唯一错过的是使用 ng-click 调用按钮上的 someFunc() 函数.

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 中多次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆