无法使用Angular.js选择多个单选按钮

时间:2016-01-02 10:55:57

标签: javascript html angularjs

我有一个问题。我无法使用Angular.js选择多个单选按钮。我正在解释下面的代码。

<div class="input-group bmargindiv1 col-md-12">
     <label class="checkbox-inline">
     <input type="radio" name="favoriteColors" ng-model="discountPrice" value="true" ng-click="disInclusive();">Discount inclusive
     </label>
     <label class="checkbox-inline">
     <input type="radio" name="favoriteColors" ng-model="discountPrice" value="false" ng-click="disInclusive();">Discount Exclusive
     </label>
 </div>
<div class="input-group bmargindiv1 col-md-12" ng-show="billdata.uncp.$valid && billdata.discount.$valid && billdata.unsp.$valid && billdata.ulsp.$valid && billdata.quantity.$valid && billdata.prcode.$valid">
  <input type="radio" name="favoriteColors" ng-model="isChecked" value="true" ng-required="!isChecked">Add new stock
  <input type="radio" name="favoriteColors" ng-model="isChecked" value="false" ng-required="!isChecked">Update stock 
</div>

我的控制器端代码如下所示。

$scope.disInclusive=function(){
        //console.log('hii',$scope.unit_sale_price==null,$scope.discount==null);
        if(($scope.unit_sale_price != '' && $scope.unit_sale_price !=null)  && ($scope.discount !='' && $scope.discount !=null)){
            //console.log('hello');
            if($scope.discountPrice=='false'){
                var price=(parseInt($scope.discount)/100)*(parseInt($scope.unit_sale_price));
                $scope.latest_sale_price=parseInt($scope.unit_sale_price)-price;
            }
            if($scope.discountPrice=='true'){
                $scope.latest_sale_price=$scope.unit_sale_price;
            }
        }else{
            //console.log('hii else');
            if($scope.discount =='' || $scope.discount ==null){
                alert('Please add the discount');
                return;
            }
            if($scope.unit_sale_price =='' || $scope.unit_sale_price ==null){
                alert('Please add the unit sale price');
                return;
            }
        }
    }

这里我有两组单选按钮。我需要选择其中一个。这里我的问题是假设我从第一组中选择了一个单选按钮,当我从第二组中选择第二个单选按钮时,第一个正在消失。在这里,我需要选择每一组中的一个。请帮助我。

1 个答案:

答案 0 :(得分:3)

您对两个组中的所有单选按钮使用了相同的名称favoriteColors。您应该为第二组中的单选按钮使用不同的名称

    <div class="input-group bmargindiv1 col-md-12">
     <label for="discount1" class="checkbox-inline"> Discount inclusive </label>
     <input type="radio" id="discount1" name="discounts" 
            ng-model="discountPrice" ng-value="true" 
            ng-click="disInclusive();">

     <label for="discount2" class="checkbox-inline"> Discount Exclusive </label>
     <input type="radio" id="discount2" name="discounts" 
            ng-model="discountPrice" ng-value="false" 
            ng-click="disInclusive();">
   </div>

  <div class="input-group bmargindiv1 col-md-12">
    <input type="radio" name="stocks" ng-model="isChecked" 
           ng-value="true" ng-required="!isChecked">Add new stock
    <input type="radio" name="stocks" ng-model="isChecked" 
           ng-value="false" ng-required="!isChecked">Update stock 
  </div>