javascript - Angularjs:仅当单选按钮被勾选时才需要文本字段。

标签 javascript html angularjs

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

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

因此,仅当选择 createNewCollection 时才需要 collectionName。

最佳答案

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 属性设置为 radio 输入的型号名称。 ng-required="selectedCollection"

Read the docs here.

关于javascript - Angularjs:仅当单选按钮被勾选时才需要文本字段。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789350/

相关文章:

javascript - Angular 6 : Prevent Browser Tooltip to show HTML tags in "innerHtml"

javascript - Angular Google map 信息窗口 HTML

javascript - 无法从 javascript/nodejs 中的全局变量检索数据

javascript - 逻辑应用绕过筛选器查询中的 Null

html - 当不在另一个元素内时定位一个元素(即所有不在段落内的 anchor )

html - 如何对齐子div中的文本

javascript - AngularJS 复选框未选中

angularjs - NgClick 位于其内部的 <li> NgClick

c# - 如何在asp.net2.0 中更改alterbox 和confirm box 按钮和图标?

javascript - 如何在树节点 CSS 上显示全宽背景色