限制多选框选择数量

                //限制酒店主题只能选三个
                $(function(){
                var num = 0;
                $(":checkbox").each(function(){
                    $(this).click(function(){
                        num = $(":checked").length;//获取打钩的个数if (num > 3) {
                            $(this).removeAttr("checked");
                        }else{
                            if($(this)[0].checked) {
                            ++num;
                            if(num > 3) {
                                //alert("最多选择 三项 的上限已满, 其他选项将会变为不可选.");
                                $(":checkbox").each(function(){
                                    if(!$(this)[0].checked) {
                                        $(this).attr("disabled", "disabled");
                                    }
                                });
                            }
                        } else {
                            --num;
                            if(num <= 2) {
                                $(":checkbox").each(function(){
                                    if(!$(this)[0].checked) {
                                        $(this).removeAttr("disabled");
                                    }
                                });
                            }
                        }
                        }
                        
                    });
                });

 

posted @ 2018-11-16 16:30  马尔科  阅读(915)  评论(0编辑  收藏  举报