如何在Angularjs中获取复选框值?

时间:2016-01-22 02:56:46

标签: javascript jquery angularjs

我有手机品牌的模特。在这里,当我提交时,我只获得模型名称。而不是那样,我需要选择品牌名称和型号名称以及输入的价格。我已经制作了这样的架构。

如何制作这样的输出数据

Categories :

nokia

sub Categories :Nokia Lumia 730 -7,000,
                Nokia 225 -5,000,
                Nokia Lumia 1020 -6,000,
                Nokia Lumia 530 -8,0000

Samsung
sub Categories:
                Samsung Galaxy A7 -10,000,
                Samsung Galaxy A3 -12,000,
                Samsung Galaxy One5 -5,000,
                Samsung Galaxy S5 Neo -6,000

HTC
sub Categories:

                HTC One M9s -9,000,
                HTC Desire 728G -12,000,
                HTC Desire 526 -4,000,

我的代码:



var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function($scope) {
	$scope.selectedBrands = [];
  
  $scope.selectBrand = function(selectedPhone) {
  console.log(selectedPhone);
  	// If we deselect the brand
  	if ($scope.selectedBrands.indexOf(selectedPhone.brandname) === -1) {
    	// Deselect all phones of that brand
    	angular.forEach($scope.phones, function(phone) {
      	if (phone.brandname === selectedPhone.brandname) {
        	phone.selected = false;
        }
      });
    }
  }
  
  $scope.checkSelectedPhones = function() {
  	var modelNames = [];
  	angular.forEach($scope.phones, function(phone) {
    	if (phone.selected) {
      	modelNames.push(phone.modelname);
      }
    });
	console.log(modelNames);
	
    console.log(modelNames.length ? modelNames.join(', ') : 'No phones selected!');
  }

	$scope.phones = [{
    id: "986745",
    brandname: "Nokia",
    modelname: "Lumia 735 TS"
  }, {
    id: "896785",
    brandname: "Nokia",
    modelname: "Nokia Asha 230"
  }, {
    id: "546785",
    brandname: "Nokia",
    modelname: "Lumia 510"
  }, {
    id: "144745",
    brandname: "Samsung",
    modelname: "Galaxy Trend 840"
  }, {
    id: "986980",
    brandname: "Samsung",
    modelname: "Galaxy A5"
  }, {
    id: "586980",
    brandname: "Samsung",
    modelname: "Galaxy Note 4 Duos"
  }, {
    id: "986980",
    brandname: "Samsung",
    modelname: "Galaxy A5"
  }, {
    id: "586980",
    brandname: "Samsung",
    modelname: "Galaxy Note Duos"
  }, {
    id: "232980",
    brandname: "Htc",
    modelname: "Htc One X9"
  }, {
    id: "456798",
    brandname: "Htc",
    modelname: "Desire 820"
  }, {
    id: "656798",
    brandname: "Htc",
    modelname: "Desire 810S"
	}];
});

myApp.filter('unique', function() {
  return function(collection, keyname) {
    var output = [], 
        keys = [];

    angular.forEach(collection, function(item) {
      var key = item[keyname];
      if(keys.indexOf(key) === -1) {
        keys.push(key);
        output.push(item);
      }
    });

    return output;
  };
});
//]]

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>
&#13;
<div ng-controller="MyCtrl">
  <button ng-click="checkSelectedPhones()">
    Check selected phones
  </button>
  
  <div ng-repeat="phone in phones | unique:'brandname'">
    <label>
      <input type="checkbox" ng-true-value="'{{phone.brandname}}'" ng-false-value="''" ng-model="selectedBrands[$index]" ng-change="selectBrand(phone)">  
      {{phone.brandname}}
    </label>
  </div>
  
  <br>
  
  <div ng-repeat="brand in selectedBrands track by $index" ng-if="brand">
    {{brand}}
    <div ng-repeat="phone in phones" ng-if="phone.brandname === brand">
      <label>
        <input type="checkbox" ng-model="phone.selected">  
        {{phone.modelname}}
		<input type="text" ng-model="price">
      </label>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

我的小提琴:demo

我需要输出这样的输出数据。

1 个答案:

答案 0 :(得分:4)

Working Demo

<div ng-repeat="brand in selectedBrands track by $index" ng-if="brand">
    {{brand}}
    <div ng-repeat="phone in phones" ng-if="phone.brandname === brand">
      <label>
        <input type="checkbox" ng-model="phone.selected">  
        {{phone.modelname}}
        <input type="text" ng-model="phone.price"> <!-- Added new property to the model phone -->
      </label>
    </div>
  </div>

并将checkSelectedPhone函数更新为

$scope.checkSelectedPhones = function() {
    var modelNames = [];
    var aletrMsg= '';
    angular.forEach($scope.phones, function(phone) {
        if (phone.selected) {
        modelNames.push(phone);
        aletrMsg += 'Brand : '+ phone.brandname + 'Phone Name: '+ phone.modelname + ' : Price: '+ phone.price +', ';
      }
    });
    alert(modelNames.length ? aletrMsg : 'No phones selected!');
  }