检查是否填写了必填字段

时间:2018-07-02 13:18:04

标签: javascript html angularjs

我有问题。我想运行一个函数,但是只有在我100%确定必须填写的字段时才能运行它。

即使没有填写必填字段,ATM也会执行该功能。

<input id="ticket-title" uib-tooltip="Insert a title for the ticket" 
       class="ticket-title" name="title" type="text"
       placeholder="Write a title for the ticket" ng-model="vm.ticket.TITLE" required>
<button type="submit" class="btn btn-default p1" ng-click="vm.createTicket()">
    Submit <i class="glyphicon glyphicon-ok"></i>
</button>

控制器

vm.createTicket = function () {
    // I need a way to check if the form/fields are filled.
};

2 个答案:

答案 0 :(得分:0)

为您的输入提供一个模型,例如:

<input id="ticket-title" ng-model="ticket" placeholder="Insert ticket title"/>

并在您的JS中执行以下操作:

if ($scope.ticket!= null && $scope.ticket!= "") {
    //do something
}

答案 1 :(得分:0)

  <input id="ticket-title" uib-tooltip="Insert a title for the ticket" 
   class="ticket-title" name="title" type="text"
   placeholder="Write a title for the ticket" ng-model="vm.ticket.TITLE" 
   required>
   <button type="submit" class="btn btn-default p1" 
    ng-disabled="vm.disableTicket()" ng-click="vm.createTicket()">
   Submit <i class="glyphicon glyphicon-ok"></i>
   </button>


  Controller :
 vm.disableTicket = function() {
   return !vm.ticket.TITLE;
 }