在angularjs问题中选择多个无线电选项

时间:2017-03-13 03:32:14

标签: angularjs html5 select checkbox radio-button

我想选择多个无线电选项。新到angularjs。

产生问题的步骤:

  1. 从第一个选择菜单中选择任意国家/地区 - >选择原因A
  2. 从第二个选择菜单中选择任意国家/地区 - >选择理由C
  3. 此处上述原因A选项消失,仅选择了原因C. 我想举出不同国家部分的两个不同原因。

    这是小提琴 http://jsfiddle.net/Lini7/knfsv64m/8/

    var app = angular.module('myApp', []);
    app.controller('myController', function ($scope) {
    $scope.showReasonA = function showReasonA(checkNoTin) {
        console.log("print the notin value from data",this.item.noTin);
        if (!this.item.country) return false;
        var selectedCountry = this.item.country.country.toLowerCase();
        var fourCountries = ["cayman islands", "albania"]
        var isIt = fourCountries.indexOf(selectedCountry) >= 0;
        console.log("print match country val",isIt);
        console.log("get checknotin value before assign",checkNoTin);
        if (checkNoTin) this.item.noTin = true;
        console.log("final checknotin val",checkNoTin);
        return isIt;
      }
    });
    

    和视图

    <body>
    <div ng-app="myApp">
      <div ng-controller="myController">
    <select name="country_0" ng-model="item.country" ng-options="" required="required" class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" style=""><option value="" selected="selected"></option><option label="AFGHANISTAN" value="object:835">AFGHANISTAN</option><option label="cayman islands" value="object:836">cayman islands</option><option label="ALBANIA" value="object:837">ALBANIA</option><option label="ALGERIA" value="object:838">ALGERIA</option><option label="AMERICAN SAMOA" value="object:839">AMERICAN SAMOA</option></select>
    <div class="row">
      <label class="checkbox">
        <input type="checkbox" ng-model="item.noTin" ng-disabled="item.tin" ng-checked="showReasonA(true)" ng-required="item.noTin" name="checkboxNoTin"/>
      </label><span translate="transactions.changeAddress.crs.yes.me_no_tin" style="margin-top: 10px" class="col-value"></span>
    </div>
    <div ng-if="item.noTin" class="row">
      <div class="col padding-top-xs"><span translate="transactions.changeAddress.crs.select_reason" style="margin-bottom: 12px;" class="pol-value col-value"></span>
        <div ng-if="item.country.tin == true" ng-init="item.reason = 0" class="row margin-bottom-medium">
          <label class="radio-button">
            <input type="radio" name="reason" ng-model="item.reason" ng-value="A" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason1"></span>
          </label>
        </div>
        <div ng-if="!showReasonA()" class="row margin-bottom-medium">
          <div class="col">
            <label class="radio-button">
              <input type="radio" name="reason" ng-model="item.reason" ng-value="B" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason2_title"></span>
              <label class="item item-input input-margin">
                <textarea  ng-model="item.othersReason" ng-required="item.reason == 2" rows="5"></textarea>
              </label>
            </label>
          </div>
        </div>
        <div ng-if="!showReasonA()" class="row margin-bottom-medium">
          <div class="col">
            <label class="radio-button">
              <input type="radio" name="reason" ng-model="item.reason" ng-value="C" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason3"></span>
            </label>
          </div>
        </div>
      </div>
    </div>
    <select name="country_0" ng-model="item.country" ng-options="" required="required" class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" style=""><option value="" selected="selected"></option><option label="AFGHANISTAN" value="object:835">AFGHANISTAN</option><option label="cayman islands" value="object:836">cayman islands</option><option label="ALBANIA" value="object:837">ALBANIA</option><option label="ALGERIA" value="object:838">ALGERIA</option><option label="AMERICAN SAMOA" value="object:839">AMERICAN SAMOA</option></select>
    <div class="row">
      <label class="checkbox">
        <input type="checkbox" ng-model="item.noTin" ng-disabled="item.tin" ng-checked="showReasonA(true)" ng-required="item.noTin" name="checkboxNoTin"/>
      </label><span translate="transactions.changeAddress.crs.yes.me_no_tin" style="margin-top: 10px" class="col-value"></span>
    </div>
    <div ng-if="item.noTin" class="row">
      <div class="col padding-top-xs"><span translate="transactions.changeAddress.crs.select_reason" style="margin-bottom: 12px;" class="pol-value col-value"></span>
        <div ng-if="item.country.tin == true" ng-init="item.reason = 0" class="row margin-bottom-medium">
          <label class="radio-button">
            <input type="radio" name="reason" ng-model="item.reason" ng-value="A" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason1"></span>
          </label>
        </div>
        <div ng-if="!showReasonA()" class="row margin-bottom-medium">
          <div class="col">
            <label class="radio-button">
              <input type="radio" name="reason" ng-model="item.reason" ng-value="B" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason2_title"></span>
              <label class="item item-input input-margin">
                <textarea ng-model="item.othersReason" ng-required="item.reason == 2" rows="5"></textarea>
              </label>
            </label>
          </div>
        </div>
        <div ng-if="!showReasonA()" class="row margin-bottom-medium">
          <div class="col">
            <label class="radio-button">
              <input type="radio" name="reason" ng-model="item.reason" ng-value="C" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason3"></span>
            </label>
          </div>
        </div>
      </div>
    </div>
    </div>
    </div>
    </body>
    

2 个答案:

答案 0 :(得分:1)

要将某些输入设为隐藏,您可以使用

<p ng-show="condition">I'm shown</p>

并且此条件值应在控制器范围内初始化

$scope.condition=true/false

隐藏节目输入,您可以使用ng-if。最好使用ng-if进行动态输入。查看详细信息here

现在针对您的情况,您可以在条件中使用item.reason来显示和隐藏您的输入。

<input type="radio" name="reason" ng-model="item.other" ng-value="C" required="required" ng-if="item.reason='object:836'" />

答案 1 :(得分:0)

终于得到了解决方案

我刚刚添加了带索引的单选按钮名称然后它工作,这将为每组按钮提供不同的名称。

  

名= '原因_ {{$索引}}'