Submit form only if at least one checkbox is checked

后端 未结 8 2085
梦如初夏
梦如初夏 2021-01-06 18:42

i\'m triyng to validate a form. In this form you\'ve to choose at least one element by checkboxes, and I can\'t be sure about their quantity (it depends by elements number).

8条回答
  •  天涯浪人
    2021-01-06 19:07

    i suggest you to use "button" instead of "submit".

    please follow this

    HTML->

    $(function(){

    $("#submit").click(function(e){
    
        var number_of_checked_checkbox= $("input[name=q]:checked").length;
        if(number_of_checked_checkbox==0){
            alert("select any one");
        }else{
            $("#booking").submit();
        }
    
             });
        });
    

提交回复
热议问题