【问题标题】:Toggle Radio buttons on click angularjs在单击 angularjs 时切换单选按钮
【发布时间】:2015-02-16 18:30:18
【问题描述】:

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

复选框 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:&lt;input type="radio" name="somethign" value="2" ng-model="checkboxSelection" ng-click="disable2='true';disable1='false';"/&gt;

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

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

【问题讨论】:

  • 如果你把ng-disabled改成ng-disabled="checkboxSelection == 1"会怎么样

标签: javascript angularjs radio-button


【解决方案1】:

更简洁的实现是根据所选值禁用选择项:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多