Jquery和angularjs获取check框选中的值小技巧

在我们平常的开发中,有时候会需要获取一下check框选中的值,以及check框选中行的所有信息。这个时候有一个小技巧那就是我们可以把要获取的信息全部放到check框的值里面,这样我们可以获取check框选中值的时候也就相当于获取了当前行的信息。

判断是否选中

[javascript] view plain copy
  1. $("input[type='checkbox']").is(':checked')  
[html] view plain copy
  1. <td><input class="states" type="checkbox"  name="orders"  value="{{e.merchantId}},{{e.orderCode}},{{e.userId}}"/></td>  

全选按钮 

[html] view plain copy
  1. <th><input class="states" type="checkbox"  id="cboxchecked" ng-click="checkAll()"/>全选</th>  

全选全不选:

[html] view plain copy
  1. var bischecked=$('#cboxchecked').is(':checked');  
  2.         var fruit=$('input[name="orders"]');  
  3.         fruit.prop('checked',bischecked);  
这里为什么要用prop而不用attr,这是因为
  • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
  • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法。
获取选中的值:
[java] view plain copy
  1. $('input[name="orders"]:checked').each(function(){  
  2.            var sfruit=$(this).val();  
  3.            var orders=sfruit.split(",");  
  4.            var reminder=new Object();  
  5.             reminder.merchantId=orders[0];  
  6.             reminder.orderCode=orders[1];  
  7.             reminder.userId=orders[2];  
  8.    
  9.             });  
angularjs实现:
使用angularjs我们不用去操作dom,我们只需要去关心这个值的状态;

首先看一下html代码:

[html] view plain copy
  1. 1 <!DOCTYPE html>  
  2.  2 <html data-ng-app="App">  
  3.  3 <head>  
  4.  4     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>  
  5.  5     <script src="script2.js"></script>  
  6.  6 </head>  
  7.  7 <body data-ng-controller="AddStyleCtrl">  
  8.  8   
  9.  9     <div>Choose Tags</div>      
  10. 10     <div>  
  11. 11         <div>You have choosen:</div>  
  12. 12         <hr>  
  13. 13         <label data-ng-repeat="selectedTag in selectedTags">  
  14. 14             (({{selectedTag}}))  
  15. 15         </label>  
  16. 16         <hr>  
  17. 17         <div data-ng-repeat="category in tagcategories">  
  18. 18             <div>{{ category.name }}</div>  
  19. 19             <div data-ng-repeat="tag in category.tags">  
  20. 20                 <div>  
  21. 21                     <input type="checkbox" id={{tag.id}} name="{{tag.name}}" ng-checked="isSelected(tag.id)" ng-click="updateSelection($event,tag.id)">  
  22. 22                     {{ tag.name }}  
  23. 23                 </div>  
  24. 24             </div>  
  25. 25             <hr>  
  26. 26         </div>  
  27. 27     </div>  
  28. 28   
  29. 29 <pre>{{selected|json}}</pre>  
  30. 30 <pre>{{selectedTags|json}}</pre>  
  31. 31   
  32. 32 </body>  
  33. 33 </html>  

line2 定义了AngularJS App;

line4 引入angularjs脚本;

line5 引入自己写的script2.js脚本;

line7 指定控制器AddStyleCtrl

line13-15 实时显示已选标签给用户看;

line17-line26 使用双重循环列出数据库(本例中就存储在了controller的一个对象里)中的数据;

  line21的这行代码作用可大了:<inputtype="checkbox" id={{tag.id}}name="{{tag.name}}" ng-checked="isSelected(tag.id)" ng-click="updateSelection($event,tag.id)">

  存储了tag的id,name,利用isSelected(tag.id)来判断是否被checked,点击时候调用updateSelection($event,tag.id)方法;

  如果想 ng-click 触发的函数里获取到该触发该函数的元素不能直接传入 this ,而需要传入scope 。我们可以传入 event.target 来获取到该元素。

line29-30 是测试时候给自己看的,可以看到selected数组和selectedTags数组中的内容;

然后看看AngularJS代码:(script2.js)

[html] view plain copy
  1. 1 /**  
  2.  2  * Created by zh on 20/05/15.  
  3.  3  */  
  4.  4 // Code goes here  
  5.  5   
  6.  6 var iApp = angular.module("App", []);  
  7.  7   
  8.  8   
  9.  9   
  10. 10 iApp.controller('AddStyleCtrl', function($scope)  
  11. 11 {  
  12. 12     $scope.tagcategories = [  
  13. 13         {  
  14. 14             id: 1,  
  15. 15             name: 'Color',  
  16. 16             tags: [  
  17. 17                 {  
  18. 18                     id:1,  
  19. 19                     name:'color1'  
  20. 20                 },  
  21. 21                 {  
  22. 22                     id:2,  
  23. 23                     name:'color2'  
  24. 24                 },  
  25. 25                 {  
  26. 26                     id:3,  
  27. 27                     name:'color3'  
  28. 28                 },  
  29. 29                 {  
  30. 30                     id:4,  
  31. 31                     name:'color4'  
  32. 32                 },  
  33. 33             ]  
  34. 34         },  
  35. 35         {  
  36. 36             id:2,  
  37. 37             name:'Cat',  
  38. 38             tags:[  
  39. 39                 {  
  40. 40                     id:5,  
  41. 41                     name:'cat1'  
  42. 42                 },  
  43. 43                 {  
  44. 44                     id:6,  
  45. 45                     name:'cat2'  
  46. 46                 },  
  47. 47             ]  
  48. 48         },  
  49. 49         {  
  50. 50             id:3,  
  51. 51             name:'Scenario',  
  52. 52             tags:[  
  53. 53                 {  
  54. 54                     id:7,  
  55. 55                     name:'Home'  
  56. 56                 },  
  57. 57                 {  
  58. 58                     id:8,  
  59. 59                     name:'Work'  
  60. 60                 },  
  61. 61             ]  
  62. 62         }  
  63. 63     ];  
  64. 64   
  65. 65     $scope.selected = [];  
  66. 66     $scope.selectedTags = [];  
  67. 67   
  68. 68     var updateSelected = function(action,id,name){  
  69. 69         if(action == 'add' && $scope.selected.indexOf(id) == -1){  
  70. 70             $scope.selected.push(id);  
  71. 71             $scope.selectedTags.push(name);  
  72. 72         }  
  73. 73         if(action == 'remove' && $scope.selected.indexOf(id)!=-1){  
  74. 74             var idx = $scope.selected.indexOf(id);  
  75. 75             $scope.selected.splice(idx,1);  
  76. 76             $scope.selectedTags.splice(idx,1);  
  77. 77         }  
  78. 78     }  
  79. 79   
  80. 80     $scope.updateSelection = function($event, id){  
  81. 81         var checkbox = $event.target;  
  82. 82         var action = (checkbox.checked?'add':'remove');  
  83. 83         updateSelected(action,id,checkbox.name);  
  84. 84     }  
  85. 85   
  86. 86     $scope.isSelected = function(id){  
  87. 87         return $scope.selected.indexOf(id)>=0;  
  88. 88     }  
  89. 89 });  

点击行的时候选中check框:
先再行上添加一个点击事件,把这个行个传到后台:
[javascript] view plain copy
  1. ng-click="checkTr(this)"  

接着给当前单元格的check框赋值;

[javascript] view plain copy
  1. <td class="text-center"><label> <input type="radio" ng-model="check" value="{{e.docGuid}}" name="check"> </label></td>  

[javascript] view plain copy
  1. $scope.checkTr=function(tr){  
  2.         $scope.check=tr.e.docGuid;  
  3.     };  

自定义复选框对应的值

默认情况下,绑定到复选框上的值是ture或者false。有时候,我们希望返回的其它值。Angular提供了一种非常好的处理方式:使用ng-ture-value和ng-false-value。

我们添加另外一组复选框,不过这时侯我们使用的不再是true或者false,而是用户自定义的值。

[html] view plain copy
  1. <!-- CUSTOM VALUE CHECKBOXES -->  
  2. <label>Personal Question</label>  
  3. <div class="checkbox">  
  4.     <label>  
  5.         <input type="checkbox" name="awesome" ng-model="formData.awesome" ng-true-value="ofCourse" ng-false-value="iWish">  
  6.         Are you awesome?  
  7.     </label>  
  8. </div>  

Angularjs全选全不选:
自己经过长时间的实践,总结出了一套自己使用Angularjs全选全不选的方式;
[javascript] view plain copy
  1. var app = angular.module('myApp', []);    
  2.     
  3. app.controller('myCart'function($scope) {    
  4.     $scope.cartList=[{    
  5.         id:0,    
  6.         image:"images/1.jpg",    
  7.         name:"Casio/卡西欧 EX-TR350",    
  8.         price:5999.88,    
  9.         num:1,    
  10.         total:5999.88,    
  11.         check:false    
  12.     },    
  13.     {       
  14.         id:1,    
  15.         image:"images/2.jpg",    
  16.         name:"Canon/佳能 PowerShot SX50 HS",    
  17.         price:3888.50,    
  18.         num:1,    
  19.         total:3888.50,    
  20.         check:false    
  21.     },    
  22.     {       
  23.         id:2,    
  24.         image:"images/3.jpg",    
  25.         name:"Sony/索尼 DSC-WX300",    
  26.         price:1428.50,    
  27.         num:1,    
  28.         total:1428.50,    
  29.         check:false    
  30.     },    
  31.     {       
  32.         id:3,    
  33.         image:"images/4.jpg",    
  34.         name:"Fujifilm/富士 instax mini 25",    
  35.         price:640.60,    
  36.         num:1,    
  37.         total:640.60,    
  38.         check:false    
  39.     }]    
  40.     $scope.checkAll=function(){  //全选全不选  
  41.             for(var i in $scope.cartList){    
  42.                  $scope.cartList[i].check=$scope.all;    
  43.             }    
  44.         }    
  45.   
  46. });  
[html] view plain copy
  1. <table id="cartTable">    
  2.        <thead>    
  3.            <tr>    
  4.                <th><label><input class="check-all check" type="checkbox" ng-click="checkAll()" ng-model="all"/> 全选</label></th>    
  5.                <th>商品</th>    
  6.                <th>单价</th>    
  7.                <th>数量</th>    
  8.                <th>小计</th>    
  9.                <th>操作</th>    
  10.            </tr>    
  11.        </thead>    
  12.        <tbody>    
  13.            <tr ng-repeat="c in cartList">    
  14.                <td class="checkbox"><input class="check-one check" type="checkbox" ng-model="c.check" /></td>    
  15.                <td class="goods"><img src="{{c.image}}" alt=""/><span>{{c.name}}</span></td>    
  16.                <td class="price">{{c.price}}</td>    
  17.                <td class="count"><span class="reduce">-</span><input class="count-input" type="text"  ng-model="c.num"/><span class="add">+</span></td>    
  18.                <td class="subtotal">{{c.total}}</td>    
  19.                <td class="operation"><span class="delete">删除</span></td>    
  20.            </tr>    
  21.        </tbody>    
  22.    </table>    
我们会在列表的List对象里面添加一个check的参数,该参数默认是false,然后我们的check框model也取check的值,所以最开始的时候页面都是全不选的状态。当我点击全选全不选的时候就去调用checkAll方法,该方法会去循环的给集合赋值,$scope.all就是我们页面选择全选反选的状态,对应的集合值发生了变化,所以页面的选择状态也会发送变化。如果要取选择的值的话,直接去遍历集合cartList,然后判断一下check状态就可以了。非常简单适用。





版权声明:学习交流群446267421 有些源代码会上传到群里,里面有很多大牛可以讨论问题。 https://blog.csdn.net/liaodehong/article/details/50531873

猜你喜欢

转载自blog.csdn.net/weixin_39816740/article/details/80690544