Angularjs 处理CheckBox处理要传输的数据

xiaoxiao2021-02-28  12

Angularjs中队CheckBox的处理:选取被选中的选项及内容,分辨选中后取消选项以及对后台数据的传输。

如下获取到要传输的数据,再进行下一步的异步处理,不做赘述。

1,HTML

<tr ng-repeat="item in totalmsg"> <input type="checkbox" name="{{item.name}}" ng-checked="isSelected(item.id)" ng-click="updateSelection($event,item.id)"/> <td>{{item.id}}</td> <td>{{item.name}}</td></tr>

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; };

转载请注明原文地址: https://www.6miu.com/read-250118.html

最新回复(0)