作者:fewtewtewt | 来源:互联网 | 2023-09-05 10:00
在我的应用程序中,我有7个复选框.我想获取所选复选框的值并存储到对象中.如果取消选择,我想在对象中删除它.
HTML
{{days}}
调节器
$scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; $scope.selectedList = {}; //this is the object to store the selected checkbox values
解决方法:
以下代码是一种简单的方法 – >检查这个plunker.这个例子为你提供了一个非常简单的KISS principle处理,用于AngularJS中多个自动生成的复选框.
模板
{{day}} Submit
领域
//default states $scope.selectDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; $scope.selectedList = {}; /** * Action */ $scope.submit = function () { angular.forEach($scope.selectedList, function (selected, day) { if (selected) { console.log(day); } }); };