切换单击angularjs上的单选按钮

Toggle Radio buttons on click angularjs

本文关键字:单选按钮 angularjs 单击      更新时间:2023-11-15

单击每个单选按钮时,我如何切换单选按钮lke应启用相应的元素,例如元素,反之亦然。我尝试过的代码:

复选框1:<input type="radio" name="somethign" value="1" ng-model="checkboxSelection" ng-click="disable1='true';disable2='false'"/>

<select ng-model="something" ng-options="r.id as r.name for r in data1" ng-disabled="disable1">

复选框2:<input type="radio" name="somethign" value="2" ng-model="checkboxSelection" ng-click="disable2='true';disable1='false';"/>

<select ng-model="something" ng-options="r.id as r.name for r in data2" ng-disabled="disable2">

我不会基于上述代码进行切换。有人能提出一个不同的选择吗?感谢

一个更干净的实现是根据所选值禁用所选项目:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-init="checkboxSelection = 1">
  <input type="radio" name="somethign" value="1" ng-model="checkboxSelection"/>First
  <input type="radio" name="somethign" value="2" ng-model="checkboxSelection"/>Second
  <br />
  <select ng-model="something1" ng-disabled="checkboxSelection != 1">
      <option>A</option>
      <option>B</option>
  </select>
  <select ng-model="something2" ng-disabled="checkboxSelection != 2">
    <option>A</option>
    <option>B</option>
  </select>
</div>

就像Tom已经提到的(这是首选方式):您可以读取复选框的模型值,并将其用于禁用的属性:

<select ng-model="something" ng-options="r.id as r.name for r in data1"    ng-disabled="checkboxSelection == 1">

当变量设置为布尔值时,您的解决方案(使用禁用变量)也应该有效:

ng-click="disable1=true;disable2=false"

或者,反过来,调整禁用的ng以显式检查字符串值,如下所示:

ng-disabled="disable1==='true'"