dwz.checkbox.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @author 张慧华 z@j-ui.com
  3. */
  4. (function($){
  5. $.fn.extend({
  6. checkboxCtrl: function(parent){
  7. return this.each(function(){
  8. var $trigger = $(this);
  9. $trigger.click(function(){
  10. var group = $trigger.attr("group");
  11. if ($trigger.is(":checkbox")) {
  12. var type = $trigger.is(":checked") ? "all" : "none";
  13. if (group) $.checkbox.select(group, type, parent);
  14. } else {
  15. if (group) $.checkbox.select(group, $trigger.attr("selectType") || "all", parent);
  16. }
  17. });
  18. });
  19. }
  20. });
  21. $.checkbox = {
  22. selectAll: function(_name, _parent){
  23. this.select(_name, "all", _parent);
  24. },
  25. unSelectAll: function(_name, _parent){
  26. this.select(_name, "none", _parent);
  27. },
  28. selectInvert: function(_name, _parent){
  29. this.select(_name, "invert", _parent);
  30. },
  31. select: function(_name, _type, _parent){
  32. var $parent = $(_parent || document),
  33. $checkboxLi = $parent.find(":checkbox[name='"+_name+"']");
  34. switch(_type){
  35. case "invert":
  36. $checkboxLi.each(function(){
  37. this.checked = !this.checked;
  38. });
  39. break;
  40. case "none":
  41. $checkboxLi.removeAttr('checked');
  42. break;
  43. default:
  44. $checkboxLi.each(function(){
  45. this.checked = true;
  46. });
  47. break;
  48. }
  49. $checkboxLi.trigger('change');
  50. }
  51. };
  52. })(jQuery);