从ng-repeat中获取复选框和收音机的价值

时间:2016-12-09 10:27:27

标签: javascript jquery angularjs

我的json值

[
    {
        "id": "583e75080c842b17e7df1628",
        "product_name": "Pizza",
        "product_price": 100,
        "product_max_quan": 10,
        "checkbox_value": [
            {
                "_id": "checkbox_1",
                "ingredientName": "chicken"
            },
            {
                "_id": "checkbox_2",
                "ingredientName": "mutton"
            }
        ],
        "radio_value": [
            {
                "_id": "radio_1",
                "ingredientName": "fish"
            },
            {
                "_id": "radio_2",
                "ingredientName": "prawn"
            }
        ]
    }
]

它们是两种类型的输入值将重复

广播框循环的

radio_value 复选框值的 checkbox_value

我想将复选框值和单选按钮值的值推送到数组

我试过

<input type="checkbox" ng-model="checkbox_value.selected" value={{checkbox_value.ingredientName}} />

$scope.save = function(){
    $scope.albumNameArray = [];
    angular.forEach($scope.checkbox_value, function(checkbox_value){
      if (checkbox_value.selected) $scope.albumNameArray.push(checkbox_value.ingredientName);
    });
  } 

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
        var app = angular.module('main', []);
                        app.controller('DemoCtrl', function ($scope,$filter) {
        
                   
                         $scope.jsondata =  [
                            {
                                "id": "583e75080c842b17e7df1628",
                                "product_name": "Pizza",
                                "product_price": 100,
                                "product_max_quan": 10,
                                "checkbox_value": [
                                    {
                                        "_id": "checkbox_1",
                                        "ingredientName": "chicken"
                                    },
                                    {
                                        "_id": "checkbox_2",
                                        "ingredientName": "mutton"
                                    }
                                ],
                                "radio_value": [
                                    {
                                        "_id": "radio_1",
                                        "ingredientName": "fish"
                                    },
                                    {
                                        "_id": "radio_2",
                                        "ingredientName": "prawn"
                                    }
                                ]
                            }
                        ];                        
                   $scope.test = {};     
                                  $scope.onchange = function(){
      	  $scope.test = $filter('filter')($scope.jsondata[0].checkbox_value, { check: true }, true);
      	}; });
                     
              
                
                
&#13;
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
       <div ng-controller="DemoCtrl" ng-app="main">
          <div ng-repeat="obj in jsondata">
             <h1> Checkbox</h1>    
              <div ng-repeat="obj1 in obj.checkbox_value">             
                  	  <input type="checkbox" ng-change="onchange()" ng-model="obj1.check" />{{obj1.ingredientName}}
               
              </div>
         
                          <p>{{test}}</p>
                           <h1> Radio</h1>
                          <div ng-repeat="obj1 in obj.radio_value">
                 <input type="radio" name="rdogroup" ng-model="$parent.mySelected" ng-value="obj1" />
                        {{obj1.ingredientName}}            
                
                        </div>
                {{mySelected}} 
                        </div>
                        <br/><br/><br/><br/>
                        </div>
&#13;
&#13;
&#13;