切换单击angularjs上的单选按钮

时间:2015-02-16 10:15:47

标签: javascript angularjs radio-button

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

CheckBox 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">

CheckBox 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">

我不会根据上面的代码进行切换。任何人都可以提出不同的选择吗?谢谢

2 个答案:

答案 0 :(得分:2)

更清洁的实现方法是根据所选值禁用select-items:

<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>

答案 1 :(得分:0)

就像汤姆已经提到的那样(这是首选方式):您可以只读取复选框的模型值并在禁用的属性中使用它:

<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-disabled以显式检查字符串值,如下所示:

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