简体   繁体   中英

get selected checkbox value in array

I have created an applicaion in angular for getting all the checkbox value which is checked into an array, without any looping, but after simultaneous checking and un-checking i am getting wrong data within the array

Working Demo

can anyone please give me some solution for this

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

app.controller('homeCtrl', function ($scope) {
    $scope.selected = [];
    $scope.array_ = angular.copy($scope.array);
    $scope.list = [{
        "id": 1,
            "value": "apple",
            "checked": false
    }, {
        "id": 3,
            "value": "orange",
            "checked": false
    }, {
        "id": 5,
            "value": "pear",
            "checked": false
    }];
    $scope.checkedOrNot = function (id, isChecked, index) {
        if (isChecked) {
            $scope.selected.push(id);
        } else {
            $scope.selected.splice(index, 1);
        }
    }
});

please see here http://jsfiddle.net/7L6beac6/2/

you need to find index of checkbox in selected array and remove it using that index instead of index of checkbox inside reaper

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

app.controller('homeCtrl', function ($scope) {
    $scope.selected = [];
    $scope.array_ = angular.copy($scope.array);
    $scope.list = [{
        "id": 1,
            "value": "apple",
            "checked": false
    }, {
        "id": 3,
            "value": "orange",
            "checked": false
    }, {
        "id": 5,
            "value": "pear",
            "checked": false
    }];
    $scope.checkedOrNot = function (id, isChecked, index) {
        console.log("index:" + index + " " + isChecked);

        if (isChecked) {
            $scope.selected.push(id);
        } else {
            var _index = $scope.selected.indexOf(id);
            $scope.selected.splice(_index, 1);
        }
    };
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM