Javascript 希望在表单加载时使用angular js禁用提交按钮
我有html表单,我想在表单启动时保持submit按钮禁用,因为默认情况下它是启用的,即使控件为空也会接受输入。 这是我的密码。 一旦输入数据,它就可以正常工作&然后将其从输入控件中删除,但默认情况下不会Javascript 希望在表单加载时使用angular js禁用提交按钮,javascript,html,angularjs,forms,Javascript,Html,Angularjs,Forms,我有html表单,我想在表单启动时保持submit按钮禁用,因为默认情况下它是启用的,即使控件为空也会接受输入。 这是我的密码。 一旦输入数据,它就可以正常工作&然后将其从输入控件中删除,但默认情况下不会 <html> <head> <title>Angular JS Forms</title> <script src = "http://ajax.googleapis.com/ajax/libs/angular
<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>
角JS形式
AngularJS示例应用程序
输入名字:
名字是必需的。
输入域名:
姓是必需的。
电邮:
电子邮件是必需的。
无效的电子邮件地址。
重置
提交
var mainApp=angular.module(“mainApp”,[]);
mainApp.controller('studentController',函数($scope){
$scope.reset=函数(){
}
$scope.reset();
});
这会有所帮助
<button ng-disabled = "studentForm.$invalid" ng-click="submit()">Submit</button>
提交
只需在
ng disabled
开头添加studentForm.firstname.$pristine | |
,这对我来说总是有效的-
<button ng-disabled = "!studentForm.firstname || !studentForm.domain || !studentForm.email">Submit</button>
提交
您需要为所有输入元素提供$pristine
<button ng-disabled = "studentForm.firstname.$pristine || studentForm.domain.$pristine || studentForm.email.$pristine || studentForm.firstname.$dirty && studentForm.firstname.$invalid || studentForm.domain.$dirty && studentForm.domain.$invalid || studentForm.email.$dirty && studentForm.email.$invalid" ng-click="submit()">Submit</button>
提交
提交监听器应该在表单上,而不是在提交按钮上,因为表单可以在不单击提交按钮的情况下提交。@RobG,我也这么认为,但显然浏览器(至少是Chrome浏览器)不允许您在禁用提交按钮的情况下使用enter等方式提交表单。但是,如果省略了submit按钮,它将允许您执行此操作。第一个表单不会提交,而第二个表单会提交。@GolezTrol Safari和Omniweb至少会提交。不管怎样,提交监听器都应该在表单上,因为一旦启用了提交按钮,就可以在所有浏览器中提交表单,而无需选择提交按钮(某些非常旧版本的IE除外)。使用此选项也可以在您只键入名称时启用提交按钮,并且不会对空电子邮件和域抛出任何错误。否,代码在这里运行得很好,我想你遗漏了一些东西Submit
问题在于,当您只需输入名字并按下选项卡时,Submit按钮将变为enable(启用)。您可以在firefox中进行检查。尝试Submit
这将使您的条件变得简短而合理