geqianqian-shihan / Alexuse

学习、测试使用
0 stars 0 forks source link

隐藏 表单记录 多选框选中 #23

Open geqianqian-shihan opened 4 years ago

geqianqian-shihan commented 4 years ago

html 隐藏表单

<input id="checkedIds" type="hidden">
// js 相关事件
.on('click','#id_table .chk',function(){
                    var $this = $(this),
                        $table = $('#id_table'),
                        $checkedIds = $('#checkedIds');
                        sts = $this.is(':checked') ? true : false,
                        isChkAll = $this.hasClass('chk_all'),
                        checkedIds = $checkedIds.attr('data-chk'),
                        chkIdsArr = (checkedIds && checkedIds.split(',')) || []
                    if(isChkAll){
                        $('#id_table .chk:not(.chk_all)').each(function (i,k) {
                            var $this_chkbox = $(this),
                                this_chk_id = $this_chkbox.val()
                                this_chk_sts = $this_chkbox.is(':checked'),
                                isInArr = $.inArray(this_chk_id,chkIdsArr);     // 没有在数组中返回 -1
                            if(sts && !this_chk_sts && -1 == isInArr){
                                chkIdsArr.push(this_chk_id);
                            }else if(!sts && this_chk_sts && -1 !== isInArr){
                                chkIdsArr.splice(isInArr,1);
                            }
                            $this_chkbox.prop("checked", sts);
                        });
                    }else{
                        var id = $this.val();
                        var chk_len = $('#id_table').find('.chk:not(.chk_all):checked').length;
                        if(sts){
                            var table_len = $('#id_table tbody').find('tr').length;
                            chkIdsArr.push(id);
                            if(chk_len == table_len){
                                $('#id_table .chk_all').prop("checked", true);
                            }
                        }else{
                            chkIdsArr.splice($.inArray(id,chkIdsArr),1);
                            if(1 > chk_len){
                                $('#id_table .chk_all').prop("checked", false);
                            }
                        }
                    }
                    $checkedIds.attr('data-chk',chkIdsArr.join());
                })