如何在JQuery中验证角度js中按钮单击时的单个字段

时间:2016-03-03 07:04:36

标签: html angularjs

由于我们有多个输入字段,并且有一些字段是必需的示例:

<input placeholder="Role" type="text" required/>
<span ng-show="Role" class="text-danger">Role is required.</span>
<input placeholder="Title" type="text" required/>
etc..
<button type="button" ngclick="Savereferences()">Save</button>

现在我想点击角度js中的“保存”按钮来验证这些字段,任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

<input placeholder="Role" ng-model="Role" type="text" required/>
<span ng-show="RoleMsg" class="text-danger">Role is required.</span>
<input placeholder="Title" ng-model="Title" type="text" required/>
etc..
<button type="button" ngclick="Savereferences()">Save</button>

用于验证角色的JS代码

$scope.Savereferences=function(){
  if($scope.Role){
                  $scope.RoleMsg=true;
                }


}

答案 1 :(得分:0)

你可以这样做...使用ng-required和form

    <form role="form" id="form" name="form" class="form-horizontal">
      <input placeholder="Role" id="role" name="role" type="text" ng-required="true"/>
      <span ng-show="form.role.$error.required">Role is required.</span>
      //Same as other field
      <button type="button" ng-disabled="!form.$valid" ngclick="Savereferences()">Save</button>
    </form>