Disable checkboxes when 6 are checked — multiple forms on page with checkbox groups

给你一囗甜甜゛ 提交于 2019-12-13 02:59:19

问题


I have multiple forms on a page, each have 30 or so checkboxes in them (the amount of forms varies depending upon the user upload). I need to have it setup when 6 checkboxes are checked the rest in that group of checkboxes that are not checked are disabled.

I can sort out doing so with one form pretty simply -- something like:

$("input:checkbox").click(function() {
      var cbk = $("input:checkbox:checked").length >= 6;    
     $("input:checkbox").not(":checked").attr("disabled",cbk);

    }); 

But this ends up disabling the checkboxes for ALL the forms. Been trying to find different ways to sort this out but not having much luck. Any and all help is appreciated. Thanks.


回答1:


Please try this:

$("input:checkbox").click(function() {
      var frm = $(this).closest("form").attr("id");//getting the id of nearest form
      var cbk = $("#" + frm + " input:checkbox:checked").length >= 6;    
     $("#" + frm + " input:checkbox").not(":checked").attr("disabled",cbk);

    }); 

logic is: Find the form in which that checkbox that was clicked resides. And use that form as a filter.

HTH



来源:https://stackoverflow.com/questions/6032693/disable-checkboxes-when-6-are-checked-multiple-forms-on-page-with-checkbox-gr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!