我有html表单,我希望在启动表单时保持提交按钮禁用,因为默认情况下它已启用并且即使控件为空也需要输入。 这是我的代码。 输入数据后,它可以正常工作。然后将其从输入控件中删除,但默认情况下不会。
<html>
<head>
<title>Angular JS Forms</title>
<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = "mainApp" ng-controller = "studentController">
<form name = "studentForm" novalidate>
<table border = "0">
<tr>
<td>Enter first name:</td>
<td><input name = "firstname" placeholder="name" type = "text" ng-model = "firstName" required>
<span style = "color:red" ng-show = "studentForm.firstname.$dirty && studentForm.firstname.$invalid">
<span ng-show = "studentForm.firstname.$error.required">First Name is required.</span>
</span>
</td>
</tr>
<tr>
<td>Enter Domain name: </td>
<td><input name = "domain" type = "text" ng-model = "domain" placeholder="domain" required>
<span style = "color:red" ng-show = "studentForm.domain.$dirty && studentForm.domain.$invalid">
<span ng-show = "studentForm.domain.$error.required">Last Name is required.</span>
</span>
</td>
</tr>
<tr>
<td>Email: </td><td><input name = "email" type = "email" ng-model = "email" placeholder="Email" length = "100" required>
<span style = "color:red" ng-show = "studentForm.email.$dirty && studentForm.email.$invalid">
<span ng-show = "studentForm.email.$error.required">Email is required.</span>
<span ng-show = "studentForm.email.$error.email">Invalid email address.</span>
</span>
</td>
</tr>
<tr>
<td>
<button ng-click = "reset()">Reset</button>
</td>
<td>
<button ng-disabled = "studentForm.firstname.$dirty &&
studentForm.firstname.$invalid || studentForm.domain.$dirty &&
studentForm.domain.$invalid || studentForm.email.$dirty &&
studentForm.email.$invalid" ng-click="submit()">Submit</button>
</td>
</tr>
</table>
</form>
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope) {
$scope.reset = function(){
}
$scope.reset();
});
</script>
</body>
</html>
答案 0 :(得分:1)
这会有所帮助,
<button ng-disabled = "studentForm.$invalid" ng-click="submit()">Submit</button>
只需在studentForm.firstname.$pristine ||
的开头添加ng-disabled
。
答案 1 :(得分:1)
您需要为所有输入元素提供$ pristine
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
<div id="consumable" data-template="testTemplate" data-bind="source:dataEntryStyle"></div>
<script type="text/x-kendo-template" id="testTemplate">
<span data-bind="text: dataEntryStyle"></span>
</script>
答案 2 :(得分:0)
这总是对我有用 -
<button ng-disabled = "!studentForm.firstname || !studentForm.domain || !studentForm.email">Submit</button>