Angularjs 处理CheckBox 处理要传输的数据
Angularjs中队CheckBox的处理:选取被选中的选项及内容,分辨选中后取消选项以及对后台数据的传输。
如下获取到要传输的数据,再进行下一步的异步处理,不做赘述。
1,HTML
ng-repeat="item in totalmsg"> type="checkbox" name="{{item.name}}" ng-checked="isSelected(item.id)" ng-click="updateSelection($event,item.id)"/>{{item.id}} {{item.name}}
2,JS
$rootScope.selected = []; $rootScope.selectedTags = []; var updateSelected = function(action,id,name){if(action == 'add' && $rootScope.selected.indexOf(id) == -1){$rootScope.selected.push(id); $rootScope.selectedTags.push(name); }if(action == 'remove' && $rootScope.selected.indexOf(id)!=-1){var idx = $scope.selected.indexOf(id); $rootScope.selected.splice(idx,1); $rootScope.selectedTags.splice(idx,1); } }; $scope.updateSelection = function($event, id){var checkbox = $event.target; var action = (checkbox.checked?'add':'remove'); updateSelected(action,id,checkbox.name); console.log($rootScope.selected); console.log($rootScope.selectedTags); }; $scope.isSelected = function(id){return $rootScope.selected.indexOf(id)>=0; };
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
