js.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. $(function(){
  2. //var list=[
  3. // { Id: 1, Desc: "小猫", IsChecked:true},
  4. // { Id: 2, Desc: "小狗", IsChecked:true},
  5. // { Id: 3, Desc: "小兔子", IsChecked:false},
  6. // { Id: 4, Desc: "小乌龟", IsChecked:false},
  7. // { Id: 5, Desc: "大西瓜", IsChecked:false},
  8. // { Id: 6, Desc: "黄河", IsChecked:false},
  9. // { Id: 7, Desc: "长江", IsChecked:true},
  10. // { Id: 8, Desc: "天安门", IsChecked:false},
  11. // { Id: 9, Desc: "神仙", IsChecked:false},
  12. // { Id: 10, Desc: "小鬼", IsChecked:false},
  13. //];
  14. //initCheckbox(list);
  15. })
  16. function initCheckbox(idKey, list) {
  17. if(list.length>0){
  18. //$('#' + idKey).prepend('<div class="choose"><button class="mybtn" id="all">全选</button><button class="mybtn" id="anti">反选</button></div><div id="checkList"></div>');
  19. $('#' + idKey).prepend('<div id="checkList"></div>');
  20. $('#' + idKey +' #all').click(function(){
  21. $('#' + idKey +' #checkList').find('input[type=checkbox]').prop('checked',true);
  22. });
  23. $('#' + idKey +' #anti').click(function(){
  24. var checklist = $('#' + idKey +' #checkList').find('input[type=checkbox]');
  25. checklist.each(function(index,item){
  26. if(checklist.eq(index).is(":checked")){
  27. checklist.eq(index).prop('checked',false);
  28. }else{
  29. checklist.eq(index).prop('checked',true);
  30. }
  31. })
  32. });
  33. list.map(function(item){
  34. if (item.IsChecked){
  35. $('#' + idKey+' #checkList').append('<label><input type="checkbox" checked=true id="'+item.Id+'"/>'+item.Desc+'</label>');
  36. }else{
  37. $('#' + idKey+' #checkList').append('<label><input type="checkbox" id="' + item.Id + '"/>' + item.Desc+'</label>');
  38. }
  39. })
  40. }
  41. }
  42. function initAndCreateCheckbox(idKey, list) {
  43. if (list.length > 0) {
  44. var content = "<div id= '" + idKey + "' class='mycheckbox'>";
  45. content += '<div id="checkList">';
  46. list.map(function (item) {
  47. if (item.IsChecked) {
  48. content += '<label><input type="checkbox" checked=true id="' + item.Id + '"/>' + item.Desc + '</label>';
  49. } else {
  50. content += '<label><input type="checkbox" id="' + item.Id + '"/>' + item.Desc + '</label>';
  51. }
  52. })
  53. content += "</div></div>";
  54. return content;
  55. }
  56. }
  57. function getChecked(){
  58. var checkboxs=$('#checkList').find('input[type=checkbox]');
  59. var checkedID=[];
  60. for(var i=0;i<checkboxs.length;i++){
  61. var current=checkboxs.eq(i);
  62. if(current.is(':checked')){
  63. checkedID.push(current.attr('id'));
  64. }
  65. }
  66. return checkedID;
  67. }
  68. function getChecked2(ctr) {
  69. var checkboxs = $('#' + ctr+' #checkList').find('input[type=checkbox]');
  70. var checkedID = [];
  71. for (var i = 0; i < checkboxs.length; i++) {
  72. var current = checkboxs.eq(i);
  73. if (current.is(':checked')) {
  74. checkedID.push(current.attr('id'));
  75. }
  76. }
  77. return checkedID;
  78. }