获得所有被选中的checkbox的值
How to get only selected checkboxes in angularjs?
by owen_pengtao
HTML
<body ng-app="">
<form ng-controller="Ctrl">
<div>You chose {{selection}}</div>
<label ng-repeat="fruitName in fruits">
<input type="checkbox" name="selectedFruits[]" value="{{fruitName}}" ng-checked="selection.indexOf(fruitName) > -1" ng-click="toggleSelection(fruitName)">{{fruitName}}</label>
</form>
</body>
JavaScript
function Ctrl($scope) {
$scope.fruits = ['apple', 'orange', 'pear', 'naartjie'];
// selected fruits
$scope.selection = [];
// toggle selection for a given fruit by name
$scope.toggleSelection = function toggleSelection(fruitName) {
var idx = $scope.selection.indexOf(fruitName);
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
$scope.selection.push(fruitName);
}
};
}