我有一个单选按钮和一个文本字段。我希望仅在选中单选按钮时才需要文本字段。
<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"
关于javascript - Angularjs:仅当单选按钮被勾选时才需要文本字段。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28789350/