JQuery限制复选框checkbox可选中个数的方法


本文实例讲述了JQuery限制复选框checkbox可选中个数的方法。分享给大家供大家参考。具体分析如下:

由于项目需要限制可批量操作的文件个数 所以写了一段小代码
如果选中个数大于允许的最大个数 其他复选框不能选择
如果小于则所有复选框都能选择

<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>

希望本文所述对大家的jQuery程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3