【问题标题】:Angularjs: Text field is only required of radio button is ticked.Angularjs:只有勾选了单选按钮才需要文本字段。
【发布时间】:2015-05-01 14:21:33
【问题描述】:

我有一个单选按钮和一个文本字段。我希望仅在选中单选按钮时才需要文本字段。

<label class="radio">
    <input type="radio" value="createNewCollection" ng-model="selectedCollection" />
</label>
<input type="text" ng-model="collectionName" placeholder="Create new Collection"/>

所以,仅在选择CreateNewCollection时才需要CollectionName。

【问题讨论】:

  • 欢迎来到 Stack Overflow!不要忘记花几分钟时间阅读导览:stackoverflow.com/tour 您的问题可能会被关闭,因为它是重复的,但我先查看并没有找到类似的问题,所以不用担心!这篇文章可能会帮助其他人找到他们正在寻找的东西。在发布新帖子之前,请务必查找其他可以解决您的问题的帖子。

标签: javascript html angularjs


【解决方案1】:

angular.module('myApp', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="form" ng-app="myApp">
  <label class="radio">
    <input type="radio" value="createNewCollection" ng-model="selectedCollection">
  </label>
  <input type="text" name="input" ng-required="selectedCollection" ng-model="collectionName" placeholder="Create new Collection">
  <p ng-show="form.input.$error.required">This is required.</p>
</form>

您需要将该输入的ng-required 属性设置为无线电输入的型号名称。 ng-required="selectedCollection"

Read the docs here.

【讨论】:

  • 这行得通,我很早就尝试使用 ng-required 和 ng-value,但它没有用。感谢新的 ngShow 指令。
猜你喜欢
  • 2019-12-13
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 2019-08-27
  • 2013-04-24
相关资源
最近更新 更多