Making sure at least one checkbox is checked

后端 未结 9 778
天命终不由人
天命终不由人 2020-11-27 16:39

I have a form with multiple checkboxes and I want to use JavaScript to make sure at least one is checked. This is what I have right now but no matter what is chosen an alert

9条回答
  •  北海茫月
    2020-11-27 17:16

    This should work:

    function valthisform()
    {
        var checkboxs=document.getElementsByName("c1");
        var okay=false;
        for(var i=0,l=checkboxs.length;i

    If you have a question about the code, just comment.


    I use l=checkboxs.length to improve the performance. See http://www.erichynds.com/javascript/javascript-loop-performance-caching-the-length-property-of-an-array/

提交回复
热议问题