jquery 全反選實現插件

jopen 9年前發布 | 2K 次閱讀 JavaScript jQuery

/*

  • selectToDo - jQuery plugin for select checkbox *
  • Copyright (c) 2014 Elric Huang *
  • Licensed under the MIT license:
  • http://www.opensource.org/licenses/mit-license.php *
  • Project home:
  • https://github.com/elrichuang/jquery.selectToDo.js *
  • Version: 0.1.3 / ;(function ($){ $.fn.selectToDo = function (options){

     var settings = $.extend({},{
         "selectAllButton"    : $("#selectAll"),
         "selectNoneButton"   : $("#selectNone"),
         "selectInvertButton" : $("#selectInv"),
     }, options);
    
     var element = this;
    
     $(settings.selectAllButton).bind("click",function(){
         element.selectAll();
     });
     $(settings.selectNoneButton).bind("click",function(){
         element.selectNone();
     });
     $(settings.selectInvertButton).bind("click",function(){
         element.selectInvert();
     });
    
     this.selectAll = function(){//全選
         element.prop('checked', true);
     };
    
     this.selectNone = function(){//全不選
         element.prop('checked', false);
     };
    
     this.selectInvert = function(){//反選
         element.each(function(){
             if(this.checked){
                 $(this).prop('checked', false);
             }else{
                 $(this).prop('checked', true);
             }
         });
     };
    
     this.result = function(){
         var checkVal=[];
         element.each(function(){
             if(this.checked){
                 checkVal.push($(this).val());
             }
         });
         if(checkVal.length > 0)
         {
             // 引用回調函數
             return checkVal.join(",");
         }else{
             return null;
         }
     };
    
     return this;
    

    }; })(jQuery);</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!