Submit form only if at least one checkbox is checked

后端 未结 8 2081
梦如初夏
梦如初夏 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:22

    Vanilla JavaScript equivalent of the jQuery way, using document.querySelector

    if (document.querySelector('.roomselect:checked')) {
        // somethings checked
    }
    

    Demo

    0 讨论(0)
  • 2021-01-06 19:29
    $("#form_id").submit(function(e) {        
        var totalChecked = $('#div_id:input[type="checkbox"]:checked').length;        
        if(totalChecked < 1)
        {            
            alert('Please select at least one checkbox before submit');
            return false;
        }  
    
    });
    
    0 讨论(0)
提交回复
热议问题