jQuery實現列表的全選功能,jquery列表全選

來源:互聯網
上載者:User

jQuery實現列表的全選功能,jquery列表全選

只需要幾行代碼就可以實現列表的全選功能,並且可以配合post請求執行背景刪除程式

js

複製代碼 代碼如下:
function DelAlert(data)
    {
        if(data == 'error')
        {
            alert("錯誤~");
        }
        else if(data == 'success')
        {
            alert("成功~");
            location.reload();
        }
    }
    //引入jquery,這裡後台配合的是thinkphp
    $(function(){
        //每行的checkbox需要有check-value屬性來存放當前行的id
        //選中全部,checkAll 為選中全部的checkbox的id selctone 為每一行checkbox的class
        $('#checkAll').selectall('selectone');
        //刪除選中,deleteAll為刪除全部按鈕的id  selectone 為每一行checkbox的class 後台接收參數ids 格式如: 1,2,3
        var url = "Home/Role/delall'";
        $('#deleteAll').delselect('selectone',url,function(data){
            DelAlert(data);
        },function(){
            layer.msg("沒有選中內容",2,0);
        });
    })

jquery.selectall.js

複製代碼 代碼如下:
(function( $ ){
    $.fn.selectall = function(className) {
        $(this).bind('click',function()
        {
            if($(this).attr('checked') == 'checked')
            {
                $(this).attr("checked",false)
                $('.'+className).attr('checked',false);
            }else{
                $(this).attr('checked','checked');
                $('.'+className).attr('checked','checked');
            }
        });
        $('.'+className).bind('click',function()
        {
            if($(this).attr('checked') == 'checked')
            {
                $(this).attr("checked",false);
            }else{
                $(this).attr('checked','checked');
            }
        });
    };
    $.fn.delselect = function(className,url,fun,unselectfun){
        $(this).bind('click',function(){
            var selectid = '';
            $("."+className).each(function(){
                if($(this).attr('checked')=='checked'){
                    selectid+=$(this).attr('check-value')+',';
                }
            });
            if(selectid)
            {
                selectid = selectid.substring(0,selectid.length-1);
                $.post(url,{ids:selectid},function(data){
                    fun(data);
                });
            }else
            {
                unselectfun();
            }
        });
    };
})( jQuery );

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.