【问题标题】:How to disable a button when there is no input in a text field当文本字段中没有输入时如何禁用按钮
【发布时间】:2017-09-09 20:40:50
【问题描述】:

如果搜索字段中没有条目,我希望禁用搜索按钮;

在我的 html 文件中,我有:

<div class="form-group">
    <input type="text" class="form-control" ng-model"searchinput">
</div>
<button type="submit" class="btn" ng-click="search()" ng-disabled="disabled"> Search</button>

在我的控制器中,我有:

if ($scope.searchinput == ""){  
     $scope.disabled = true;
}

这不起作用。我做错了什么?

【问题讨论】:

  • 试试 ng-disabled="!searchinput"
  • 它确实有帮助......工作得很好。 :)
  • 由于某种原因,我的评论没有显示在这里。昨天我确实评论过你的建议很有帮助。

标签: angularjs angularjs-forms


【解决方案1】:

如果长度小于零,则计算searchinput 的长度,然后简单地禁用

isButtonDisable = function() {
   $scope.searchinput.length == 0 ? true : false;
}

在你的 html 中

<div class="form-group">
    <input type="text" class="form-control" ng-model"searchinput">
</div>
<button type="submit" class="btn" ng-click="search()" ng-disabled="isButtonDisable()"> Search</button>

【讨论】:

    【解决方案2】:

    只需这样做:

    HTML:

       <div class="form-group">
           <input type="text" class="form-control" ng-model"searchinput">
       </div>
       <button type="submit" class="btn" ng-disabled="!searchinput"> Search</button>    
    

    这会解决你的问题。

    【讨论】:

      【解决方案3】:

      试试这个:

          <form name="myForm" ng-controller="MainCtrl">
          <input type="text" ng-required="true" name='myName' ng-model='myName'/>
          <span>{{name}}</span>
          <input type="submit" value="Submit"  ng-disabled="myForm.myName.$pristine" />
          {{myForm.myName.$pristine}}
      </form>
      

      【讨论】:

      • 为什么这么难,其实很容易做到的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      相关资源
      最近更新 更多