繁体   English   中英

无法通过单个按钮触发angularJS中的提交

[英]Unable to trigger submit in angularJS from a single button

使用ng-submit我有一个表单提交回调,即submitFN 有2个按钮:

  • 资助我的创业公司!
  • 重启

我想要的输出是单击“重置”按钮,输入字段为0。单击“资助我的启动”后,它将显示警报。 如何做到这一点?

问题 :两个按钮都触发了SubmitFN。 无法重设。

代码

 <!DOCTYPE html> <html> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"> </script> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('startUpController', function($scope) { $scope.funding = { startingEstimate: 0 }; $scope.calculate = function () { $scope.funding.needed = $scope.funding.startingEstimate * 10; }; $scope.submitFN = function () { window.alert('Go and find more customers.') } $scope.resetFN = function () { $scope.startingEstimate = 0; } }); </script> </head> <body> <div ng-app='mainApp'> <form ng-submit="submitFN()" ng-controller="startUpController"> Starting: <input type="text" ng-change="calculate()" ng-model="funding.startingEstimate"/> Recommended: {{funding.needed}} <button>Fund my startup!</button> <button ng-click="resetFN()">Reset</button> </form> </div> </body> </html> 

我完全不是angularJS的新手,任何帮助都是非常宝贵的。 提前致谢。

更新我尝试的代码之一:

 <!DOCTYPE html> <html> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"> </script> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('startUpController', function($scope) { $scope.funding = { startingEstimate: 0 }; $scope.calculate = function () { $scope.funding.needed = $scope.funding.startingEstimate * 10; }; $scope.submitFN = function () { window.alert('Go and find more customers.') } $scope.resetFN = function () { $scope.startingEstimate = 0; } }); </script> </head> <body> <div ng-app='mainApp'> <form ng-submit="submitFN()" ng-controller="startUpController"> Starting: <input type="text" ng-change="calculate()" ng-model="funding.startingEstimate"/> Recommended: {{funding.needed}} <button>Fund my startup!</button> <button type = "button" ng-click="resetFN()">Reset</button> </form> </div> </body> </html> 

默认情况下,按钮type为Submit,因此第二个按钮也具有按钮类型submit 如果您不想在该按钮上提交表单,则应明确将该按钮类型设置为button

type="button"

您也有在resetFN代码中的错误,应该修改分配给ng-model的值

    $scope.resetFN = function () {
        $scope.funding.startingEstimate = 0;
    }

更正的代码如下:

 <!DOCTYPE html> <html> <head> <title></title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"> </script> <script> var mainApp = angular.module("mainApp", []); mainApp.controller('startUpController', function($scope) { $scope.funding = { startingEstimate: 0 }; $scope.calculate = function () { $scope.funding.needed = $scope.funding.startingEstimate * 10; }; $scope.submitFN = function () { window.alert('Go and find more customers.') } $scope.resetFN = function () { $scope.funding.startingEstimate = 0; } }); </script> </head> <body> <div ng-app='mainApp'> <form ng-submit="submitFN()" ng-controller="startUpController"> Starting: <input type="text" ng-change="calculate()" ng-model="funding.startingEstimate"/> Recommended: {{funding.needed}} <button>Fund my startup!</button> <button type="button" ng-click="resetFN()">Reset</button> </form> </div> </body> </html> 

你错过funding

$scope.resetFN = function () {
   $scope.funding.startingEstimate = 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM