Limit Number of Checkboxes Checked

后端 未结 5 1844
温柔的废话
温柔的废话 2021-01-14 20:16

I have a form with several checkboxes. I have three categories of checkboxes in the form. I need to limit to a max of three checkboxes per category.

I used

5条回答
  •  终归单人心
    2021-01-14 20:48

    This is a newbie code with javascript. It's like working...hardly... I call this function on each input. But there is surely a way to call this function one time. It's working but uncheking a random checkbox from thoses who call it.

       Education
        Religions
        News
    
    
    
      function just2cat()
            {
                var allInp = document.getElementsByTagName('input');
                const MAX_CHECK_ = 2; /*How many checkbox could be checked*/
                var nbChk =0;
                for(var i= 0; i MAX_CHECK_) 
                        {
                            alert("At most 2 categories can be chosen");
                            allInp[i].checked=false; /* try to Unchek the current checkbox :'(  */
                        }
    
                    }
                }
            }
    

提交回复
热议问题