【问题标题】:Required field Validation on input on image button click必填字段验证输入图像按钮单击
【发布时间】:2016-03-02 06:34:12
【问题描述】:

当我在buttontype submit 上进行验证时,验证工作正常。但是,我有一个图像,对我来说就像一个图像按钮,我希望在用户单击图像之前填充项目名称。有人可以帮我实现这个吗?

  <form name="userForm">
            <table>
                <tr>
                    <td>
 <%-- <button type="submit" data-ng-click="addAnswers($event)" data-ng-disabled="userForm.$invalid">Save</button>--%>
                    <img src="/SaveBttn.png"
                            style="width: 100px; height: 50px;" data-ng-click="addAnswers($event)" />
                    </td>
                    <td>
                        <img alt="" onclick="Close()" src="/Close.png"
                            style="width: 100px; height: 50px;" />
                    </td>
                </tr>
            </table>
            <div>
            <input type="text" required name="ProjectName" placeholder="ProjectName"  data-ng-model="name" maxlength="250" style="width: 400px;" />
            </div>  
</form>​

【问题讨论】:

    标签: angularjs angularjs-validation


    【解决方案1】:

    您必须在 addAnswers 操作中设置有效性。

    var app = angular.module("app",[]);
    
    app.controller("ctrl",function($scope){
      
      $scope.name = "";
      $scope.addAnswers = function($event,userForm){
        
        if($scope.name == ""){
           $scope.userForm["ProjectName"].$setValidity('empty', false);
          }
        else{
           $scope.userForm["ProjectName"].$setValidity('empty', true);
         
          }
        
        }
      
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="app" ng-controller="ctrl">
    <form name="userForm">
    
                    <table>
                        <tr>
                            <td>
                            <img src="/SaveBttn.png"
                                    style="width: 100px; height: 50px;" data-ng-click="addAnswers($event)" />
                            </td>
                            <td>
                                <img alt="" onclick="Close()" src="/Close.png"
                                    style="width: 100px; height: 50px;" />
                            </td>
                        </tr>
                    </table>
            <div>
            <input type="text" required name="ProjectName" ng-model="name" placeholder="ProjectName" />
              <span ng-show="userForm.ProjectName.$error.empty">This feild is empty.</span>
    </div>           
      </form>
      </div>

    【讨论】:

    • @Janet 你看到我的回答了吗?
    【解决方案2】:

    如果这不起作用,只需将其包装在 &lt;a&gt;

    <a data-ng-click="addAnswers($event)" style="text-decoration: none !important">
         <img src="/SaveBttn.png" style="width: 100px; height: 50px;" />
    </a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 2016-04-03
      • 2017-08-14
      • 2023-03-09
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2020-02-04
      相关资源
      最近更新 更多