baigoCheckall.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. v0.1.0 jQuery baigoCheckall plugin 表单全选插件
  3. (c) 2014 baigo studio - http://www.baigo.net/jQueryPlugins/baigoCheckall/
  4. */
  5. (function($){
  6. $.fn.baigoCheckall = function(){
  7. var thisForm = $(this);
  8. $(thisForm).find(":checkbox").click(function(){
  9. var _child = $(this).attr("id"); //根据id设置子对象
  10. var _parent = $(this).attr("class"); //根据class设置父对象
  11. child_check(_child); //设置子对象
  12. parent_check(_parent); //设置父对象
  13. });
  14. //设置父对象
  15. var parent_check = function(_parent){
  16. var _parent_num = $(thisForm).find("#" + _parent).size(); //获取父对象数量
  17. if(_parent_num > 0){ //如果有父对象
  18. var _brother_num = $(thisForm).find("." + _parent).size(); //根据parent获取兄弟对象数
  19. var _brother_checked_num = $(thisForm).find("." + _parent + ":checked").size(); //根据parent获取兄弟对象选中数
  20. if(_brother_num > 0 && _brother_checked_num < _brother_num){ //如果有兄弟对象且兄弟对象选中数小于实际数,则设置父对象未选中
  21. $(thisForm).find("#" + _parent).removeAttr("checked");
  22. } else {
  23. $(thisForm).find("#" + _parent).prop("checked", "checked");
  24. }
  25. var _parent_this = $(thisForm).find("#" + _parent).attr("class"); //根据该父对象的parent获取爷对象
  26. parent_check(_parent_this); //设置爷对象
  27. }
  28. };
  29. //设置子对象
  30. var child_check = function(_child){
  31. var _child_obj = $(thisForm).find("." + _child); //获取子对象
  32. var _checked = $(thisForm).find("#" + _child).prop("checked"); //获取父对象的选中状态
  33. if(_child_obj){ //如果有子对象
  34. _child_obj.each(function(){ //遍历
  35. var _disabled = $(this).attr("disabled");
  36. if(_checked){ //根据父对象的选中状态,设置子对象的选中状态
  37. if (_disabled){
  38. $(this).removeAttr("checked");
  39. } else {
  40. $(this).prop("checked", "checked");
  41. }
  42. } else {
  43. $(this).removeAttr("checked");
  44. }
  45. var _child_this = $(this).attr("id"); //根据该子对象的id获取孙对象
  46. child_check(_child_this); //设置孙对象
  47. });
  48. }
  49. };
  50. };
  51. })(jQuery);