JQuery limits the number of checkboxes checkbox can select

  • 2020-06-01 08:09:54
  • OfStack

This example demonstrates how JQuery limits the number of checkboxes checkbox can select. Share with you for your reference. The specific analysis is as follows:

Because the project needs to limit the number of files that can be batch operation, I wrote a small piece of code
No other check box can be selected if the number of checks is greater than the maximum allowed
If less than, all check boxes can be selected


<script type="text/javascript">
 $(document).ready(function() {
  $('input[type=checkbox]').click(function() {
   $("input[name='apk[]']").attr('disabled', true);
   if ($("input[name='apk[]']:checked").length >= 3) {
    $("input[name='apk[]']:checked").attr('disabled', false);
   } else {
    $("input[name='apk[]']").attr('disabled', false);
   }
  });
 })
</script>
<ul>
 <li>
  <input type="checkbox" name="apk[]" value=1 />
  APK1
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=2 />
  APK2
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=1 />
  APK3
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=4 />
  APK4
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=6 />
  APK5
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=7 />
  APK6
 </li>
 <li>
  <input type="checkbox" name="apk[]" value=8 />
  APK7
 </li>
</ul>

I hope this article has been helpful to your jQuery programming.


Related articles: