在文本字段中未输入任何值时,按钮保持活动状态

时间:2017-05-04 13:48:23

标签: javascript html angularjs

我正在处理我要禁用提交按钮的页面,直到在文本框中输入所有值,但是当页面加载时,提交按钮已经处于活动状态

.directive('passwordVerify', function() {
    return {
        restrict: 'A',
        require: '?ngModel',
        link: function(scope, elem, attrs, ngModel) {
            scope.$watch(attrs.ngModel, function() {
                if (scope.confirm_password === scope.user_password) {
                    scope.pw.confirm_password.$setValidity('passwordVerify', true);
                    scope.pw.user_password.$setValidity('passwordVerify', true);
                } else if (scope.confirm_password !== scope.user_password) {
                    scope.pw.confirm_password.$setValidity('passwordVerify', false);
                    scope.pw.user_password.$setValidity('passwordVerify', false);
                }
            });
        }
     };
})

HTML

<div class="list">
<form id="create" name="pw" method="post">
  <label class="item item-input item-stacked-label">
    <span class="input-label">Username</span>
    <input type="text" placeholder="kojobaah" ng-model="username" required>
</label>

   <label for="password">New Password
    <input type="password" name="user_password" ng-model="user_password" ng-required="confirm_password && !user-password" password-verify="confirm_pasword">
    <p ng-show="pw.user_password.$error.passwordVerify">Passwords do not match</p>
    <p ng-show="pw.user_password.$error.required">This field is required</p>
  </label>
</p>
 <p>
  <label for="password">Confirm Password
    <input type="password" name="confirm_password" ng-model="confirm_password" ng-required="user_password && !confirm_password" password-verify="user_password"> 
    <p ng-show="pw.confirm_password.$error.passwordVerify">Passwords do not match</p>
    <p ng-show="pw.confirm_password.$error.required">This field is required</p>
  </label>
  <br />
  <p align="center"><button ng-disabled="create.$invalid" class="button button-balanced" ng-click="register()">Create Account</button></p>  
  </form>
  </div>

2 个答案:

答案 0 :(得分:1)

在:

<button ng-disabled="pw.$invalid" class="button button-balanced" 
                ng-click="register()">Create Account</button>

create.$invalid更改为pw.$invalid

使用名称form而非id

答案 1 :(得分:0)

不确定它到底会发生什么,但这是我发现的,它会禁用提交按钮:

function myFunction() {
    document.getElementById("mySubmit").disabled = true;

一旦输入所有值,可能必须“调整”以使您的代码处于活动状态。不确定这是否有帮助