Fork me on GitHub

js中数组的添加和移除

   1.引入js文件

<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>

2.HTML中代码的书写
<td><input  type="checkbox" ng-click="updateSelection($event,entity.id)"></td>
$event 代表该行是否被选中
entity.id为自己需要传入的值


3.js代码书写
//定义一个数组
$scope.selectIds=[];
$scope.updateSelection=function($event,id){
if($event.target.checked){
//往数组中加入一个元素
$scope.selectIds.push(id);
}else{
//删除数组中一个元素
var i=$event.selectIds.indexOf(id);
$scope.selectIds.splice(i,1);
}
}
备注:先定义一个数组 selectIds ,如果选中,则往数组中添加一个元素,否则,移除一个元素。


posted @ 2020-09-06 16:12  晨度  阅读(899)  评论(0编辑  收藏  举报