jquery checkbox 限制多选的个数

2015年11月6日 16:32:49

选中第四个的时候提示超过了3个, 点解alert框取消后, 将最后一个选中的checkbox取消选中

 1 <script>
 2     $(document).ready(function (){
 3         $('input[type=checkbox]').click(function(){
 4             if ($("input[name='contract[]']:checked").length > 3) {
 5                 alert('最多选3个');
 6                 $(this).removeAttr("checked");
 7             }
 8         });
 9     });
10 </script>